ClassPathResource resource = new ClassPathResource("Fonts/simhei.ttf");
PdfFont watermark = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
watermark = PdfFontFactory.createFont(resource.getFile().getAbsolutePath(), PdfEncodings.IDENTITY_H,false);
PdfEncodings.IDENTITY_H,false);
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
//添加标题
本地正常到Linux下就找不到了
在网上查了好久,说法五花八门也没一个能行的
我的最后这样解决的:
直接以字节流传过去就没问题了
private static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
public static ByteArrayOutputStream PaperTestDetailedToPdfUtil(DetailedToPdfVo checkContentInfo) {
ClassPathResource resource = new ClassPathResource("Fonts/simhei.ttf");
PdfFont watermark = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// watermark = PdfFontFactory.createFont(resource.getFile().getAbsolutePath(), PdfEncodings.IDENTITY_H,false);
watermark = PdfFontFactory.createFont(ToPdfUtils.toByteArray(resource.getInputStream()) , PdfEncodings.IDENTITY_H,false);
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
...
...
}