Last active
April 13, 2021 12:52
-
-
Save aspose-com-gists/2d3fbe9b1c9c0d333a0b87e6232ebd58 to your computer and use it in GitHub Desktop.
HTML到Java的PDF转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建HTML加载选项 | |
HtmlLoadOptions htmloptions = new HtmlLoadOptions(); | |
// 载入HTML档案 | |
Document doc = new Document("HTML-Document.html", htmloptions); | |
// 将HTML文件转换为PDF | |
doc.save("HTML-to-PDF.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 创建和初始化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