iText는 html을 통해 pdf(중국어, css 포함)를 생성합니다.

1884 단어
        
            com.itextpdf
            itextpdf
            5.5.11
        
        
            com.itextpdf
            itext-asian
            5.2.0
        
        
            com.itextpdf.tool
            xmlworker
            5.5.11
        
  @RequestMapping("pdf")
  public void pdf(HttpServletRequest request, HttpServletResponse response, String html) {
    Document document = new Document(PageSize.LETTER);
    try {
      response.setContentType("application/pdf");
      PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
      document.open();
      InputStream htmlStream = new ByteArrayInputStream(html.getBytes());
      InputStream cssStream = request.getServletContext().getResourceAsStream("/resources/css/pdf.css");
      XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlStream , cssStream , new XMLWorkerFontProvider() {
        @Override
        public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
          BaseFont bf = null;
          try {
            bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
          } catch (Exception e) {
            e.printStackTrace();
          }
          Font font = new Font(bf, size, style, color);
          font.setColor(color);
          return font;
        }
      });
    } catch (DocumentException | IOException e) {
      e.printStackTrace();
    }
    document.close();
  }

좋은 웹페이지 즐겨찾기