我有一个使用java 8 + maven的web应用程序,带有eclipse neon和angular 2+。应用服务器是ibm是9。
为了制作pdf,我使用了itextpdf,但出于某种原因,重音符号被替换为:
名字=>名字
国籍=>国籍
这是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);
});
}
如何通过休息发送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;
}
我试过的:
在pom.xml的属性中添加这两行对我来说很有用
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>