当前位置: 首页 > 知识库问答 >
问题:

iText:用Java将html和css转换为PDF

刘畅
2023-03-14

我正在尝试将HTML转换为PDF。首先,我从下面的链接将我的HTML代码转换为XHTML。http://www.cruto.com/resources/code-generators/code-converters/html-to-xhtml.asp

然后,为了测试它,我用生成的XHTML代码创建了一个HTML文件,并成功地显示在浏览器上。之后,我尝试用下面的java代码将HTML文件转换为PDF。

public static final String DEST = "C:/Users/Turgut/Desktop/test12.pdf";
public static final String[] HTML = { "C:/Users/Turgut/Desktop/New Text Document (5).html", "C:/Users/Turgut/Desktop/New Text Document (5).html" };

public static void main(String[] args) {

    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new TestHtmlToPdf().createPdf(DEST);
}

public void createPdf(String file) {
    Document document = new Document();
    try {
        //String HTML = "C:/Users/Turgut/Desktop/test12.html";
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        String css = readCSS();
        for (String htmlfile : HTML) {
            String html = Utilities.readFileToString(htmlfile);
            ElementList list = XMLWorkerHelper.parseToElementList(html, css);
            for (Element e : list) {
                document.add(e);
            }
            document.newPage();
        }
        document.close();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    catch(DocumentException ex) {
        ex.printStackTrace();
    }
}

private String readCSS() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    InputStream is = XMLWorkerHelper.class.getResourceAsStream("/default.css");
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }
    return new String(baos.toByteArray());
}

我在head标签中有一个style标签,如下所示。

<style type="text/css">
                body {
                background-color: #FFFFFF;
                font-family: 'Tahoma', "Times New Roman", Times, serif;
                font-size: 11px;
                color: #666666;
                }
                h1, h2 {
                padding-bottom: 3px;
                padding-top: 3px;
                margin-bottom: 5px;
                color : #000000;
                text-transform: uppercase;
                font-family: Arial, Helvetica, sans-serif;
                }
                h5 {
                padding-bottom: 0px;
                padding-top: 0px;
                margin-top: 0px;
                margin-bottom: 0px;
                color : #000000;
                font-style: normal;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 1em;
                text-transform:none;
                }
                h5x {
                padding-bottom: 0px;
                padding-top: 0px;
                margin-top: 0px;
                margin-bottom: 0px;
                color : #000000;
                font-style: bold;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 1em;
                text-transform:none;
                }                   
                h6 {
                padding-bottom: 0px;
                padding-top: 0px;
                margin-top: 0px;
                margin-bottom: 0px;
                color : #666666;
                font-style: normal;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 1em;
                text-transform:none;
                }

                h1 {
                font-size: 1.4em;
                text-transform:none;
                }
                h2 {
                font-size: 1em;
                color: brown;
                }
                h3 {
                font-size: 1em;
                color: #333333;
                text-align: justify;
                margin: 0;
                padding: 0;
                }
                h4 {
                font-size: 1.4em;
                font-style: bold;
                font-family: Arial, Helvetica, sans-serif;
                color: #000000;
                margin: 0;
                padding: 0;
                }
                h4x {
                font-size: 1.4em;
                font-style: bold;
                font-family: Arial, Helvetica, sans-serif;
                color: #666666;
                margin: 0;
                padding: 0;
                }
                hr {
                height:2px;
                color: #000000;
                background-color: #000000;
                border-bottom: 1px solid #000000;
                }
                p, ul, ol {
                margin-top: 1.5em;
                }
                ul, ol {
                margin-left: 3em;
                }
                blockquote {
                margin-left: 3em;
                margin-right: 3em;
                font-style: italic;
                }
                a {
                text-decoration: none;
                color: #70A300;
                }
                h7 {
                font-size: 1.1em;
                font-style: bold;
                font-family: Arial, Helvetica, sans-serif;
                color: #000000;
                margin: 0;
                padding: 0;
                }
                a:hover {
                border: none;
                color: #70A300;
                }
                #customerPartyTable {
                border-width: 1px;
                border-spacing: 0px;
                border-style: solid;
                border-color: #FFFFFF; 
                border-collapse: collapse;
                background-color: #FFFFFF
                }
                #lineTable {
                border-width:2px;
                border-spacing:;
                border-style: solid;
                border-color: #000000; 
                border-collapse: collapse;
                background-color:;
                }
                #lineTableTd {
                border-width: 1px; 
                padding: 1px;
                border-style:  none solid none none;
                border-color: black;
                background-color: white;
                }
                #lineTableTh {
                border-width: 1px; 
                padding: 1px;
                border-style:  none solid none none;
                background-color: white;
                }
                #lineTableTrx {
                border-width: 0px; 
                padding: 0px;
                border-style: solid;
                border-color: #000000;
                background-color: white;
                -moz-border-radius:;
                }
                #lineTableThx {
                border-width: 1px; 
                padding: 1px;
                border-style:  none solid solid none;
                background-color: white;
                }
                #lineTableTr {
                border-width: 0px; 
                padding: 0px;
                border-style: solid;
                border-color: #000000;
                background-color: white;
                -moz-border-radius:;
                }
                #lineTableBudgetTd {
                border-width: 0px;  
                border-spacing:0px;
                padding: 1px;
                border-style: solid;
                border-color: #000000; 
                background-color: white;
                -moz-border-radius:;
                }                    
                table {
                border-spacing:0px;
                }
                td {
                border-color:#000000; 
                }</style>

从HTML文件生成PDF文件是没有问题的,但我不能读取css块,我猜。因为,PDF文件和HTML文件是不一样的。在PDF文件中,一些文本的颜色与HTML文件不同。

如何用CSS生成PDF文件?谢谢你的建议。

共有1个答案

孙熠彤
2023-03-14
    ByteArrayInputStream html = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(htmlSource)));
    ByteArrayInputStream css = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(cssSource)));
    html = getHtmlByteArrayStream(); //this is only for my picture not neccessary 
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destPdf));
    writer.setInitialLeading(12);
    // step 3
    document.open();

    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, html, css);

    // step 5
    document.close();

我就是这样做的。

 类似资料:
  • 我想使用itext 2.1.7将一个html页面转换为pdf。我已经使用HTMLWorker来转换html文件,但它没有采用我在html中使用的内联css。下面是我的代码片段。谁能帮助解决这个问题… 提前道谢!

  • 我想用iText将带有图像的html文件转换成pdf格式。我在这里提供我的消息来源。 请帮助我如何使用iText将带有图像的html文件转换为pdf格式。如果没有图像或者硬编码图像路径,我可以转换html文件。提前致谢

  • 将html文件转换为pdf文件。我有html文件,css文件和js文件在一个文件夹,我如何转换index.html创建pdf使用Java的itext。有谁能帮我解决这个问题。有没有样本项目?

  • 问题内容: 我有一个HTML(不是XHTML)文档,可以在Firefox 3和IE 7中很好地呈现。它使用相当基本的CSS对其进行样式设置,并在HTML中很好地呈现。 我现在正在寻找一种将其转换为PDF的方法。我努力了: [DOMPDF]:表格有很大的问题。我考虑了我的大型嵌套表并对其进行了帮助(在此之前,它只消耗了128M的内存然后就死了-这就是我对php.ini中的内存的限制),但是它使表完全

  • 我想知道关于如何使用Java将HTML和XHTML网页转换为PDF的一些好的教程或逻辑是什么? 还有建议我如何使用Java将html、bootstrap css转换为pdf?

  • 我正在遵循这个位置中指定的示例,将现有的HTML转换为可访问的PDF。 https://developers.itextpdf.com/de/content/itext-7-examples/itext-7-converting-html-pdf/pdfhtml-accessible-pdf-创建 有没有一种方法可以将CSS作为字符串传递给PDF转换器,而不是在html文件本身中指定它?我需要根据