public class GeneratePDF { public static void main(String[] args) { try { String k = "<html><body> This is my Project </body></html>"; OutputStream file = new FileOutputStream(new File("E:\\Test11.pdf")); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, file); document.open(); InputStream is = new ByteArrayInputStream(k.getBytes()); XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); document.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } } }
这是我的代码,用于将HTML转换为静态和小内容HTML的Pdf,可以正常工作,但是对于动态和长内容的HTML,则为com.itextpdf.tool.xml.exceptions.RuntimeWorkerException。
com.itextpdf.tool.xml.exceptions.RuntimeWorkerException
问题是您的HTML无效。
尝试使用HTMLWorker类进行转换