Java 缩小HTML页面并转换为A4大小的pdf
将html页面转换为pdf。当我将页面转换为pdf时,html页面内容不适合A4页面。我希望将整个html页面转换为pdf格式,而不丢失任何内容。 我已经有了将html转换为pdf的代码Java 缩小HTML页面并转换为A4大小的pdf,java,html,pdf,pdf-writer,Java,Html,Pdf,Pdf Writer,将html页面转换为pdf。当我将页面转换为pdf时,html页面内容不适合A4页面。我希望将整个html页面转换为pdf格式,而不丢失任何内容。 我已经有了将html转换为pdf的代码 String tempOutFileNameXHTML = "/Users/Common/index" + i + DOT + "xhtml"; String tempOutFileNamePDF = "/Users/Common/index" + i + DOT
String tempOutFileNameXHTML = "/Users/Common/index" + i + DOT + "xhtml";
String tempOutFileNamePDF = "/Users/Common/index" + i + DOT + PDF;
File targetFile = targetFiles.get(i); // gets all the specified html files
CleanerProperties props = new CleanerProperties();
props.setTranslateSpecialEntities(true);
props.setTransResCharsToNCR(true);
props.setOmitComments(true);
//checking of starting and ending tags are in proper
HtmlCleaner htmlCleaner = new HtmlCleaner(props);
TagNode tagNode = htmlCleaner.clean(targetFile);
PrettyXmlSerializer prettyXmlSerializer = new PrettyXmlSerializer(props);
prettyXmlSerializer.writeToFile(tagNode, tempOutFileNameXHTML, "utf-8");
File xhtmlpath = new File(tempOutFileNameXHTML);
File pdfPath = new File(tempOutFileNamePDF);
com.itextpdf.text.Document pdfDocument = null;
PdfWriter pdfWriter = null;
pdfDocument = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4); // TODO handle this
pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfPath));
pdfDocument.open();
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 25, Font.BOLD);
pdfDocument.add(new Paragraph("Target : " + targetFile.getParentFile().getName(), catFont));
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, (com.itextpdf.text.Document) pdfDocument,
new FileInputStream(xhtmlpath), null);
pdfDocument.close();
这里的问题是当html页面宽度太长时,它不适合A4大小的pdf。是否有办法缩小页面宽度或使其成为pdf而不丢失任何内容?
我该怎么做?Html页面是否可以缩小并转换为pdf?“XHTML渲染器项目支持将XHTML输出到PDF。请看一个示例。”
>iText可能支持CSS样式。您可以使用样式表格式化HTML页面,使其适合一个页面。有关样式表和iText的一些信息:
如前所述,您也可以尝试飞碟,它是另一个HTML到PDF的转换器。至少在飞碟上,你可以提供真正的样式表,CSS支持也相当不错。看看这篇文章。可能会有帮助