带有 html 的 OpenPDF

问题描述 投票:0回答:1

我需要将 html 内容转换为 pdf 的帮助。

我目前正在使用 OpenPDF 1.3.30 在 Java 中生成我的 pdf 文件。我有一个使用 Vue Editor 的 vue 组件,它是 Quill 的 Vue 富文本编辑器包装器。

目前我正在将 html 内容作为字符串发送到我的后端类,并使用 jsoup 和 openhtmltopdf 将 html 内容转换为 pdf 文档。我想将内容添加到我正在生成的 pdf 文档中,该文档还包含其他内容。我想将内容添加到 PdfPTable。

我被困在如何将内容添加到正在生成的 pdf 中,以及如何应用我的资源中包含的 quill css 样式。

这是我的代码:

此代码来自我生成的转换 html 内容的类。

` public PdfPTable buildComments(int mBottom) throws IOException {
        PdfPTable table = new PdfPTable(1);
        try {
                reportPrintUtilities.setCellGrey(cellGrey);
                reportPrintUtilities.setReport(report);
            
                table.setTotalWidth(width);
                table.setLockedWidth(true);

                try {
                    String htmlContent = "<div class=\"ql-editor\">" + report.getComment() + "</div>";
                    createHtmlFile();
                    table.addCell(reportPrintUtilities.buildCell(new Phrase("Additional Comments", headFont), 1, 1, 18, "center", 2, 2, 1, 1, true, false));
                    buildCommentHtmlFile(htmlContent);
                    File htmlFile = new File(HTML_FILE);
                    org.jsoup.nodes.Document doc = createWellFormedHtml(htmlFile);
                    xhtmlToPdf(doc, PDF_OUTPUT);

                    //table.addCell(cell);

                    //Element qlEditor = (Element) doc.body().getElementsByClass("ql-editor");

                    //String extractedText = extractTextFromPdf(PDF_OUTPUT);
                    PdfPCell cell = new PdfPCell(new Paragraph(doc.body().html()));
                    table.addCell(cell);

                    //htmlFile.delete();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                table.setSpacingBefore(5F);
        } catch(DocumentException de) {
            throw new ExceptionConverter(de);
        }
        return table;
    }

    public void createHtmlFile() throws IOException {
        File file = new File(HTML_FILE);
        if (!file.exists()) file.createNewFile();
        StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<!DOCTYPE html>\n" +
                "<head>\n" +
                CSS_FILE +"\n"+
                " <style>\n" +
                "  body {\n" +
                "   margin: 0;\n" +
                "   padding: 0;\n" +
                "  }\n" +
                "  p, h1, h2, h3 {\n" +
                "   margin: 0;\n" +
                "   padding: 0;\n" +
                "  }\n" +
                " </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "</body>\n" +
                "</html>");

        FileWriter writer = new FileWriter(file);
        writer.write(htmlBuilder.toString());
        writer.close();
    }

    public void buildCommentHtmlFile(String htmlString) throws IOException {
        File inputHTML = new File(HTML_FILE);
        org.jsoup.nodes.Document doc = Jsoup.parse(inputHTML, "UTF-8");

        org.jsoup.nodes.Element body = doc.body();
        body.append(htmlString);



        FileWriter writer = new FileWriter(inputHTML);
        writer.write(doc.outerHtml());
        writer.close();
    }

    private org.jsoup.nodes.Document createWellFormedHtml(File inputHTML) throws IOException {
        org.jsoup.nodes.Document document = Jsoup.parse(inputHTML, "UTF-8");
        document.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
        return document;
    }

    private void xhtmlToPdf(org.jsoup.nodes.Document doc, String outputPdf) throws IOException {
        try (OutputStream os = new FileOutputStream(outputPdf)) {
            String baseUri = FileSystems.getDefault()
                    .getPath("src/main/resources/")
                    .toUri()
                    .toString();
            PdfRendererBuilder builder = new PdfRendererBuilder();

            builder.withUri(outputPdf);
            builder.toStream(os);

            builder.withW3cDocument(new W3CDom().fromJsoup(doc), baseUri);
            builder.run();
        }
    }

Here is part of my code that generates the pdf:


        Rectangle pdfLayout = PageSize.A4;

        ReportHeaderFooter headerFooter = new ReportHeaderFooter();
        headerFooter.setCloudfront(cloudfront);
        headerFooter.setReport(report);
        headerFooter.setCellGrey(cellGrey);


        Document document = new Document(pdfLayout, 5, 5, 85, mBottom);
        float width = document.getPageSize().getWidth() - 10;

        ReportSectionOne reportSectionOne = new ReportSectionOne();
        reportSectionOne.setReport(report);
        reportSectionOne.setCellGrey(cellGrey);
        reportSectionOne.setWidth(width);

        String testDate = new SimpleDateFormat("yyyy-MM-dd").format(report.getTestDate());
        String fileName = report.getReportNo() + "-" + report.getMethodType() + "-" + testDate +   ".pdf";

        reportPrintUtilities.setCloudfront(cloudfront);
        reportPrintUtilities.setReport(report);
        reportPrintUtilities.setCellGrey(cellGrey);

        File pdfFile = new File(fileName);
        FileOutputStream fos = new FileOutputStream(pdfFile);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.open();
        writer.setFullCompression();
        writer.setPageEvent(headerFooter);
        document.open();
        
                // Comments
        if(!report.getComment().isEmpty()){
            ReportComments reportComments = new ReportComments();
            reportComments.setReport(report);
            reportComments.setWidth(width);
            reportComments.setCellGrey(cellGrey);
            reportComments.setCloudfront(cloudfront);
            reportComments.setDocument(document);
            document.add(reportComments.buildComments(mBottom));

        }

任何建议将不胜感激。

java jsoup quill openpdf openhtmltopdf
1个回答
0
投票

嘿,我通常这样做。

final StringReader reader = new StringReader(<your html in string>);
                HtmlParser.parse(document, reader); 

这是最终代码。

        try {
            try (Document document = new Document()) {
                document.setPageSize(PageSize.LETTER);
                PdfWriter.getInstance(document, new FileOutputStream("openpdfdemo/src/main/resources/output/openpdf.pdf"));
                document.open();
                final StringReader reader = new StringReader(TemplateParseUtils.generateHTMLDocument(TEMPLATE_CORREO_LIBRANZA));
                HtmlParser.parse(document, reader);
            } catch (DocumentException | IOException de) {
                System.err.println(de.getMessage());
            }
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        }
    } 

但是有一个问题是生成嵌入在你的 html 中的 css 样式。

Open PDF 无法生成样式。

我通常得到这个

出于这个原因,我不得不找到另一个图书馆

© www.soinside.com 2019 - 2024. All rights reserved.