Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 13, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/2d3fbe9b1c9c0d333a0b87e6232ebd58 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2d3fbe9b1c9c0d333a0b87e6232ebd58 to your computer and use it in GitHub Desktop.
HTML到Java的PDF转换
// 创建HTML加载选项
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// 载入HTML档案
Document doc = new Document("HTML-Document.html", htmloptions);
// 将HTML文件转换为PDF
doc.save("HTML-to-PDF.pdf");
// 创建HTML加载选项
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// 将转换设置为单页PDF
htmloptions.setRenderToSinglePage(true);
// 载入HTML档案
Document doc = new Document("HTML-Document.html", htmloptions);
// 将HTML文件另存为PDF
doc.save("HTML-to-PDF.pdf");
// 创建HTML加载选项
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// 载入HTML档案
Document doc = new Document("HTML-Document.html", htmloptions);
// 设置密码和加密PDF文档
doc.encrypt("userpassword", "ownerPassword", Permissions.ModifyContent , CryptoAlgorithm.AESx256);
// 将HTML文件另存为PDF
doc.save("HTML-to-PDF.pdf");
// 创建和初始化URL
URL oracleURL = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html");
// 获取网页作为输入流
InputStream is = oracleURL.openStream();
// 初始化HTML加载选项
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// 将流加载到Document对象中
Document pdfDocument = new Document(is, htmloptions);
// 将输出另存为PDF格式
pdfDocument.save("HTML-to-PDF.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment