Jak wygenerować plik PDF w Node.js
Wykorzystaj pełny potencjał generowania plików PDF w swoich aplikacjach Node.js, oglądając nasz samouczek, w którym dowiesz się, jak przekształcać HTML, strony internetowe i obrazy w dynamiczne pliki PDF za pomocą IronPDF, oraz odkryjesz, jak to potężne narzędzie może usprawnić proces tworzenia oprogramowania — zapisz się już teraz i rozpocznij bezpłatny okres próbny!
W tym obszernym samouczku zagłębiamy się w generowanie plików PDF w Node.js przy użyciu biblioteki IronPDF. Zaczynamy od zainstalowania niezbędnych pakietów i skonfigurowania środowiska. Samouczek obejmuje importowanie niezbędnych modułów, w tym PDF Generator, PDF Document i IronPDF Global Config, a także modułu FS z Node.js. Pokazujemy tworzenie plików PDF z ciągów znaków HTML poprzez konwersję prostego tagu HTML "Hello World" na plik PDF. Następnie przyjrzymy się konwersji adresu URL strony Wikipedii poświęconej plikom PDF na dokument PDF, pokazując możliwości IronPDF w zakresie szczegółowego przechwytywania stron internetowych. Samouczek obejmuje również konwersję obrazów do pojedynczego pliku PDF poprzez filtrowanie plików JPEG z katalogu i wykorzystanie funkcji konwersji obrazów do formatu PDF w programie PDF Generator. Uruchamiając kod w terminalu, z powodzeniem generujemy i przeglądamy pliki PDF, pokazując możliwości IronPDF w zakresie konwersji różnych źródeł na dokumenty PDF. To potężne narzędzie znacznie rozszerza możliwości generowania plików PDF w aplikacjach Node.js. Aby uzyskać więcej samouczków i poznać funkcje IronPDF, zasubskrybuj kanał Iron Software i zarejestruj się na wersję próbną, aby osobiście wypróbować oprogramowanie.
# Bash code to install the IronPDF library in your Node.js project.npm install ironpdf# Bash code to install the IronPDF library in your Node.js project.npm install ironpdf// Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages();// Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages();




