【发布时间】:2019-02-21 13:51:28
【问题描述】:
我有一个使用 java 8 + maven 和 eclipse neon 和 angular 2+ 的 web 应用程序。应用服务器是 ibm 是 9.
为了制作 pdf,我使用 itextpdf 但由于某种原因,重音被替换为:
Prénom => Prénom
国民 => 国民
这里是 Angular 部分
generatePDF(obj, options) {
return this.http.get('/pdf', options)
.map((response: Response) => {
const pdfBlob = response.blob();
const blob = new Blob(['\ufeff', pdfBlob], {
type: 'application/pdf'
});
const pdfName = this.pdfName.transform(obj);
FileSaver.saveAs(blob, pdfName);
});
}
如何通过rest发送pdf
@GET
@Path("/pdf")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response pdf(@QueryParam("id") String id) {
ByteArrayOutputStream bos;
try {
bos = pdfBr.generatePdf(id);
final ByteArrayInputStream responseStream = new ByteArrayInputStream(bos.toByteArray());
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
StreamingOutput output = new StreamingOutput() {
@Override
public void write(java.io.OutputStream out) throws IOException,
WebApplicationException {
int length;
byte[] buffer = new byte[1024];
while((length = responseStream.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
responseStream.close();
}
};
return Response.ok(output).header(
"Content-Disposition", "attachment;filename=\"" + "pdf_" + id + ".pdf" + "\"").build();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return Response.ok().build();
pdf是如何创建的
@Override
public ByteArrayOutputStream generatePdf(String id)
throws DocumentException, MalformedURLException, IOException {
Obj obj = objDao.getObj(id);
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);
document.open();
PdfPTable info = new PdfPTable(new float[] { 100 });
info.getDefaultCell().setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
info.setWidthPercentage(100);
final Image logo = Image.getInstance(getClass().getClassLoader().getResource("/bar.png"));
logo.scalePercent(0.75f);
PdfPCell logoCell = new PdfPCell(logo, true);
logoCell.setBorderWidth(0f);
logoCell.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
info.addCell(logoCell);
document.add(info);
document.add(new Phrase("\n", FontFactory.getFont(FontFactory.HELVETICA, 4, Font.BOLD)));
PdfPTable objInfo = new PdfPTable(new float[] { 20, 15, 15, 15, 20, 15 });
objInfo.setWidthPercentage(100);
objInfo.getDefaultCell().setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
Font labelFont = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.BOLD | Font.ITALIC);
Font valueFont = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);
Font valueFontRed = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL, BaseColor.RED);
Font valueFontGreen = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL, BaseColor.GREEN);
objInfo.addCell(new Paragraph("Nom :", labelFont));
objInfo.addCell(new Paragraph(obj.getLastName(), valueFont));
objInfo.addCell(new Paragraph("Prénom :", labelFont));
objInfo.addCell(new Paragraph(obj.getFirstName(), valueFont));
document.close();
pdfWriter.close();
baos.close();
return baos;
}
我尝试了什么:
将 java 文件的编码更改为 utf-8
将项目的编码更改为 uft-8
将 encoding.properties 文件更改为 9
更改 Eclipse 的内容类型
【问题讨论】:
-
会不会是您的代码是 UTF-8 编码的,但您的编译器需要像 Latin-1 这样的东西?
-
已经将每次出现的 ISO-**** 替换为 UTF-8 并且 maven 的文档说编码的默认值是 UTF-8...
-
本地执行是否有效?也许您的整个环境与它无关......
标签: java angular maven encoding itext