将html页面转换为pdf。当我将页面转换为 pdf 时,html 页面内容不适合 A4 页面。我希望将整个 html 页面转换为 pdf 而不会丢失任何内容。
我已经有了将 html 转换为 pdf 的代码
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 吗?关于如何做到这一点的任何想法?