使用PDFbox将HTML 转为PDF
时间: 2023-08-06 22:09:22 浏览: 690
你可以使用 Apache PDFBox 库将 HTML 转换为 PDF。下面是一个使用 PDFBox 的示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
public class HTMLtoPDFConverter {
public static void main(String[] args) {
String htmlFilePath = "path/to/input.html";
String pdfFilePath = "path/to/output.pdf";
// Load HTML file
File htmlFile = new File(htmlFilePath);
try {
// Create a new PDF document
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// Create a PDPageContentStream object
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Load the HTML file into a PDStream
FileInputStream inputStream = new FileInputStream(htmlFile);
PDStream pdStream = new PDStream(document, inputStream);
// Set the media box of the page
PDRectangle mediaBox = page.getMediaBox();
contentStream.addRect(mediaBox.getLowerLeftX(), mediaBox.getLowerLeftY(),
mediaBox.getWidth(), mediaBox.getHeight());
contentStream.clip();
// Create a PDF renderer
PDFRenderer renderer = new PDFRenderer(document);
renderer.setDocument(document);
// Render the HTML file to PDF
renderer.renderPageToStream(renderer.getCurrentPageNo(), pdStream);
// Close the content stream and the document
contentStream.close();
document.save(new FileOutputStream(pdfFilePath));
document.close();
System.out.println("HTML converted to PDF successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请确保将 `path/to/input.html` 替换为要转换的 HTML 文件的路径,将 `path/to/output.pdf` 替换为保存生成的 PDF 文件的路径。运行此代码将生成一个包含 HTML 内容的 PDF 文件。
阅读全文