当我将html转换为pdf时,我设置了enfding utf-8,当我转换pdf时,长英语单词或长数字不能换行,但是中文可以。
我很困惑这个问题几个小时。
这是我的html文件:
html中有一些中文因为只是一个测试。你可以忽略它
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>award system</title>
<meta name="description" content="" />
<link rel="stylesheet" href="part.css">
</head>
<body class="page-header-fixed">
<h2 class="part-title">七 Certificate Catalogue of Core intellectual property</h2>
<table style="table-layout:fixed;white-space:normal;word-wrap:break-word;word-break:break-all;" >
<tbody>
<tr >
<td class="table-item" data-name="type">Invention Patent</td>
<td class="table-item" data-name="name">一种访问网站的方法和系统</td>
<td class="table-item" data-name="country">chinese</td>
<td class="table-item" data-name="auth">ZL201310169965.9</td>
<td class="table-item" data-name="date">2017-11-03</td>
<td class="table-item" ><p style="white-space:normal;word-wrap:break-word;word-break:break-all;">2680796</p></td>
<td class="table-item" data-name="obligee">www.ctbri.commmmmmmmmmmmmm</td>
<td class="table-item" data-name="inventor">name</td>
<td class="table-item" data-name="status">Authorized</td>
</tr>
</tbody>
</table>
</body>
</html>
这是css文件
body {
background: none !important;
}
.title {
text-align: center;
font-size: 24px;
}
.subtitle {
text-align: center;
font-size: 16px;
}
.part-title {
text-align: center;
font-size: 18px;
}
table {
width: 80%;
border-collapse: collapse;
border-left: 1px solid #000;
margin: 0 auto 50px;
}
table td {
border: 1px solid #000;
border-left: none;
height: 40px;
text-align: center;
font-size: 14px;
}
table .item-name {
width: 20%;
}
table .align-left {
text-align: left;
}
table p {
margin: 0;
}
这是我的java代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.css.media.MediaDeviceDescription;
import com.itextpdf.html2pdf.css.media.MediaType;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.font.FontProvider;
public class Pdf0010HtmlToPdf {
public static void main(String[] args) {
// baseuri:the parent path of html/css/javasript
String baseuri = "G:/TPractice/workOne/TransforePdf/src/main/resources/pdf0010htmlTopdf/";
// html source file
String desc = "G:/TPractice/workOne/TransforePdf/src/main/resources/pdf0010htmlTopdf/problem.pdf";
File htmlSource = new File(baseuri + "problem.html");
// pdfHTML specific code
ConverterProperties converterProperties = new ConverterProperties();
try {
FileOutputStream outputStream = new FileOutputStream(desc);
WriterProperties writerProperties = new WriterProperties();
writerProperties.addXmpMetadata();
PdfWriter writer = new PdfWriter(outputStream);
PdfDocument pdf = new PdfDocument(writer);
pdf.setTagged();
PageSize pageSize = new PageSize(575, 1500);
pdf.setDefaultPageSize(pageSize);
FontProvider font = new FontProvider();
font.addStandardPdfFonts();
font.addFont("C:\\Windows\\Fonts\\simhei.ttf");// set my font as chinese font
converterProperties.setFontProvider(font);
converterProperties.setBaseUri(baseuri);
MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
converterProperties.setMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdf, converterProperties);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("OK!");
}
}
答案 0 :(得分:1)
//HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdf, converterProperties);//change this use below
List<IElement> elements =HtmlConverter.convertToElements(new FileInputStream(htmlSource), converterProperties);
Document document = new Document(pdfDoc);
document.setProperty(Property.SPLIT_CHARACTERS,new DefaultSplitCharacters(){
@Override
public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
//return super.isSplitCharacter(text, glyphPos);//override this
return true;//解决word-break: break-all;不兼容的问题
}
});
for (IElement element : elements) {
document.add((IBlockElement)element);
}
document.close();