import {PdfDocument} from "@ironsoftware/ironpdf";
(async () => {
// Create a PDF from an HTML string
const pdf = await PdfDocument.fromHtml("<h1>Hello World</h1>");
// Export the PDF to a file
await pdf.saveAs("output.pdf");
// Advanced Example with HTML Assets
// Load external HTML assets: Images, CSS, and JavaScript.
const htmlContentWithAssets = "<img src='icons/iron.png'>";
const advancedPdf = await PdfDocument.fromHtml(htmlContentWithAssets);
// Save the PDF with loaded assets
await advancedPdf.saveAs("html-with-assets.pdf");
})();
import {PdfDocument} from "@ironsoftware/ironpdf";
(async () => {
// Render the HTML file
const pdf = await PdfDocument.fromHtml("example.html");
// Export the PDF document
await pdf.saveAs("output.pdf");
})();
import {PdfDocument} from "@ironsoftware/ironpdf";
(async () => {
// Render the web URL to PDF
const pdf = await PdfDocument.fromUrl("https://ironpdf.com/");
// Export the PDF document
await pdf.saveAs("url.pdf");
})();
图像转换为PDF
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
});
})();