Bài viết này sẽ chỉ cho bạn cách tạo PDF từ HTML bằng Java. Bạn có thể dễ dàng thao tác và hiển thị tài liệu HTML bao gồm các kiểu CSS.
Aspose.HTML for Java
cho phép các ứng dụng của bạn thực hiện một loạt các tác vụ thao tác HTML và hỗ trợ các tính năng HTML5, CSS3, SVG và HTML Canvas. Với API của chúng tôi, bạn có thể tải các trang web, phân tích, chỉnh sửa nội dung và lưu cũng như chuyển đổi các trang.
Các định dạng phổ biến mà bạn có thể thao tác: XHTML, MHTML, SVG, MD, PDF, XPS, EPUB, PNG, TIFF, JPEG, BMP và các định dạng khác.
PDF từ HTML trong Java
Bộ tính năng phong phú
Aspose.HTML for Java cung cấp cho bạn rất nhiều tính năng trong nhiều lĩnh vực khác nhau:
• Mô hình đối tượng tài liệu - cho phép bạn chỉnh sửa, tạo và xóa các nút, và được xây dựng theo tài liệu HTML chính thức.
• Bộ chuyển đổi hiệu suất cao. Để biết thêm thông tin về các định dạng tệp được hỗ trợ, hãy truy cập Danh sách tính năng
hoặc các bài viết Chuyển đổi
.
• Xử lý CSS, JavaScript và các định dạng tích hợp khác.
Tạo PDF từ HTML bằng một dòng mã Java
Bạn có thể dễ dàng tạo PDF từ HTML trong ứng dụng Java của mình chỉ bằng một dòng mã duy nhất bằng cách sử dụng các phương thức tĩnh của lớp Converter
.
// Invoke the ConvertHTML method to convert the HTML to PDF.
com.aspose.html.converters.Converter.convertHTML(
"<span>Hello World!!</span>",
".",
new com.aspose.html.saving.PdfSaveOptions(),
"output.pdf"
);
Tạo PDF từ HTML từng dòng bằng Java
Dưới đây là các bước để tạo PDF từ HTML từng dòng:
- Sử dụng lớp HtmlDocument
để tải tệp HTML đầu tiên.
- Sau đó tạo một thể hiện của PdfSaveOptions
.
- Với phương thức ConvertHTML()
của lớp Converter
lưu HTML dưới dạng tệp PDF. Bạn sẽ cần truyền vào HTMLDocument
, PdfSaveOptions
và đường dẫn tệp đầu ra cho phương thức ConvertHTML() để tạo PDF từ HTML.
// Prepare an HTML code and save it to the file.
String code = "<span>Hello World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
// Initialize PdfSaveOptions
com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
// Convert HTML to PDF
com.aspose.html.converters.Converter.convertHTML(
document,
options,
"output.pdf"
);
} finally {
if (document != null) {
document.dispose();
}
}
Tùy chỉnh quá trình render với tùy chọn Save
Bạn có thể tùy chỉnh quá trình render bằng PdfSaveOptions
, và chỉ định kích thước trang
, quyền tệp
, lề
, loại media CSS
, và nhiều hơn nữa. Trong ví dụ dưới đây bạn có thể thấy cách sử dụng PdfSaveOptions để tạo tệp PDF với kích thước trang tùy chỉnh và màu nền:
// Prepare an HTML code and save it to the file
String code = "<span>Hello</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Set A5 as a page-size and change the background color to green
com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
com.aspose.html.drawing.Page anyPage = new com.aspose.html.drawing.Page();
anyPage.setSize(
new com.aspose.html.drawing.Size(
com.aspose.html.drawing.Length.fromInches(8.3f),
com.aspose.html.drawing.Length.fromInches(5.8f)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(com.aspose.html.drawing.Color.getGreen());
// Convert HTML document to PDF
com.aspose.html.converters.Converter.convertHTML(
"document.html",
options,
"output.pdf"
);
Trong bài viết Fine-Tuning Converters
, bạn có thể tìm hiểu thêm về PdfSaveOptions
.
Nhà cung cấp luồng đầu ra
Bạn có thể triển khai giao diện MemoryStreamProvider
nếu muốn lưu tệp vào bộ nhớ từ xa, như đám mây hoặc cơ sở dữ liệu.
Giao diện MemoryStreamProvider được thiết kế như một đối tượng callback để tạo luồng và giải phóng luồng đã tạo sớm sau khi render trang hoặc tài liệu.
Chúng tôi sẽ trình bày một ví dụ về việc sử dụng MemoryStreamProvider dưới đây:
package com.aspose.html.examples.java;
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
public class MemoryStreamProvider implements java.io.Closeable {
// List of InputStream objects created during the document rendering
public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();
@Override
public void close() throws java.io.IOException {
for (java.io.InputStream stream : lStream) {
stream.close();
}
}
}
// Create an instance of MemoryStreamProvider
try (MemoryStreamProvider streamProvider = new MemoryStreamProvider()) {
// Initialize an HTML document
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("<span>Hello World!!</span>", ".");
try {
// Convert HTML to PDF by using the MemoryStreamProvider
com.aspose.html.converters.Converter.convertHTML(
document,
new com.aspose.html.saving.PdfSaveOptions(),
streamProvider.lStream
);
// Get access to the memory stream that contains the result data
java.io.InputStream inputStream = streamProvider.lStream.stream().findFirst().get();
// Flush the result data to the output file
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("output.pdf")) {
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
fileOutputStream.write(buffer);
}
} finally {
if (document != null) {
document.dispose();
}
}
}
Aspose cung cấp hỗ trợ kỹ thuật cho tất cả các sản phẩm của mình, từ bài viết trong cơ sở kiến thức
, tài liệu
, blog
, mẫu mã
, và bản demo
, đến Hỗ trợ miễn phí
cũng như Hỗ trợ trả phí
có sẵn cao.
Nếu bạn cần các chuyên gia làm việc cùng bạn và giúp bạn triển khai Aspose.HTML for Java theo nhu cầu, giải pháp tốt nhất là dịch vụ Tư vấn trả phí
.