编码由itextpdf

时间:2019-02-21 13:51:29

标签: java angular maven encoding itext

我有一个使用Java 8 + Maven和Eclipse Neon和Angle 2+的Web应用程序。应用服务器的ibm是9。

要制作pdf,我使用itextpdf,但是由于某些原因,重音符号是这样替换的:

Prénom=>Prénom

Nationalité=>Nationalité

这里是角部分

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;
}

我尝试了什么:

  • 将Java文件的编码更改为utf-8

  • 将项目的编码更改为uft-8

  • 更改9中的encoding.properties文件

  • 更改日食的内容类型

1 个答案:

答案 0 :(得分:0)

在pom.xml的属性中添加这两行对我有用

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>