将范围报告HTML文件转换为PDF

时间:2018-10-03 15:00:54

标签: java pdf itext

我正在尝试将范围报告HTML文件转换为PDF,但是我没有成功。

下面是我尝试的代码。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class Demo 
{
public static void main( String[] args ) throws DocumentException,     IOException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("html.html")); 
document.close();
System.out.println( "PDF Created!" );
}
}

Exception in thread "main" com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag link.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:134)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:396)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:236)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:214)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:175)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:238)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:210)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:183)
at com.tib.controlStatements.Demo.main(Demo.java:22)

HTML文件链接:

https://drive.google.com/open?id=1UrHafoit0rJuhTC0QRqCe9bC5PMpqIWS

2 个答案:

答案 0 :(得分:0)

尝试以下示例。您没有创建文件并尝试填充它。

export PYTHONPATH=/home/myname/.local/bin

答案 1 :(得分:-1)

最后找到了解决方法。因此,我要做的是添加了一个对html进行了一些调整的css文件,并能够隐蔽为PDF并附带屏幕截图进行打印。