目录
2. Flying Saucer (XHTML/CSS to PDF)
1. Apache PDFBox
Apache PDFBox 是一个开源库,用于创建和操作 PDF 文档。
添加依赖
如果使用 Maven,可以在 pom.xml
中添加:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version> <!-- 请根据需要检查最新版本 -->
</dependency>
示例代码(如果说图片位置展示不对,可以调整宽高)
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXImage;
import java.io.File;
import java.io.IOException;
public class ImageToPdfWithPdfBox {
public static void main(String[] args) {
String[] imagePaths = {"path/to/image1.jpg", "path/to/image2.png"};
String outputPdfPath = "output.pdf";
try {
convertImagesToPdf(imagePaths, outputPdfPath);
System.out.println("PDF created successfully!");
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
public static void convertImagesToPdf(String[] imagePaths, String outputPdfPath) throws IOException {
try {
PDDocument document = new PDDocument();
for (String imagePath : imagePaths) {
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, document);
PDPage page = new PDPage();
document.addPage(page);
// 获取页面的宽度和高度
float pageWidth = page.getMediaBox().getWidth();
float pageHeight = page.getMediaBox().getHeight();
// 获取图片的宽度和高度
float imgWidth = pdImage.getWidth();
float imgHeight = pdImage.getHeight();
// 计算比例以适应页面
float scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
// 计算新的图像尺寸
float newWidth = imgWidth * scale;
float newHeight = imgHeight * scale;
// 使用 PDPageContentStream 来绘制图像
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.drawImage(pdImage, (pageWidth - newWidth) / 2+60, (pageHeight - newHeight) / 2+60, newWidth-120, newHeight-120);
}
}
// 保存文档
document.save(outputPdfPath);
// 关闭文档
document.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
2. Flying Saucer (XHTML/CSS to PDF)
Flying Saucer 是一个用于将 XHTML 和 CSS 渲染为 PDF 的库。
添加依赖
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>1.1.20</version>
</dependency>
示例代码(如果图片展示有问题,可以调整图片宽高)
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.ITextUserAgent;
import java.io.File;
import java.io.FileOutputStream;
public class HtmlToPdf {
public static void main(String[] args) {
String html = "<html><body><img src='path/to/image1.jpg' width="500" height="800"/></body></html>";
String outputPdfPath = "output.pdf";
try (FileOutputStream os = new FileOutputStream(new File(outputPdfPath))) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(os);
System.out.println("PDF created successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.PDF 中的图转图
public static void pfd2Image(){
try {
PDDocument document = PDDocument.load(new File("output23.pdf"));
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300);
ImageIO.write(bim, "PNG", new File("output_page_" + (page + 1) + ".png"));
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
选择合适的库
选择哪个库取决于你的具体需求:
- 简单性: 如果只是需要将图片转换成 PDF,
Apache PDFBox
或iText
是简单且常用的选择。 - HTML 支持: 如果需要处理 HTML 和 CSS,
Flying Saucer
是一个不错的选择。