import {PdfGenerator} from "@ironsoftware/ironpdf";
import fs from 'fs';
(async () => {
// Specify the directory path
const directoryPath = './images';
// Read the contents of the directory
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
// Filter file names to include only .jpg and .jpeg extensions
const jpegFiles = files.filter((file) =>
file.toLowerCase().endsWith('.jpg') || file.toLowerCase().endsWith('.jpeg')
);
// Construct full file paths for the filtered files
const filePaths = jpegFiles.map((file) => `${directoryPath}/${file}`);
// Converts the images to a PDF and save it.
const pdf = PdfGenerator.imageToPdf(filePaths).then(
(returnedPdf)=> {
returnedPdf.saveAs("composite.pdf");
});
// Also see PdfDocument.rasterizeToImageFiles() method to flatten a PDF to images or thumbnails
});
})();
图像转换为PDF
要将单张图像转换为 PDF 文档,只需在 PdfGenerator.imageToPdf 方法中输入文件路径,然后导出生成的 PDF 文档。
将多个图像转换为 PDF 的过程则不同,因为你需要一个文件路径数组。使用 readdir 方法获取图像文件所在的目录路径。