PDF to HTML Node.js Examples
Complete Node.js examples for common PDF to HTML tasks. See the Node.js SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Node.js example can be copied and run independently.
Basic examples
Choose file, in-memory, or output-stream handling for the HTML result. Stream examples use a binary file stream as their destination.
The memory variants return UTF-8 bytes. Decode them when your application needs a text string.
Convert a PDF file
Upload an existing logo.pdf file.
Save to a file
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.convertFileToFile("logo.pdf", "logo.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Keep in memory
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); const chunks = []; let failed = false; client.convertFile("logo.pdf", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const html_bytes = Buffer.concat(chunks); // Buffer console.log("HTML ready:", html_bytes.length, "bytes"); } });
Write to a stream
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const { pipeline } = require('node:stream'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; console.error(error.toString()); process.exitCode = 1; if (output_stream) output_stream.destroy(); } try { client.convertFile("logo.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } output_stream = fs.createWriteStream("logo.html"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "logo.html"); }); }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { console.log("Conversion response ended."); }, }); } catch (error) { fail(error); }
Convert a URL
Fetch a PDF from its URL.
Save to a file
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); } );
Keep in memory
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); const chunks = []; let failed = false; client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const html_bytes = Buffer.concat(chunks); // Buffer console.log("HTML ready:", html_bytes.length, "bytes"); } });
Write to a stream
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const { pipeline } = require('node:stream'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; console.error(error.toString()); process.exitCode = 1; if (output_stream) output_stream.destroy(); } try { client.convertUrl("https://your-server.com/invoice.pdf", { data: (stream) => { if (failed) { stream.destroy(); return; } output_stream = fs.createWriteStream("invoice.html"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "invoice.html"); }); }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { console.log("Conversion response ended."); }, }); } catch (error) { fail(error); }
Convert raw PDF bytes
Read logo.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer client.convertRawDataToFile(pdf_data_1, "logo.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Keep in memory
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer const chunks = []; let failed = false; client.convertRawData(pdf_data_1, { data: (stream) => stream.on("data", (chunk) => chunks.push(chunk)), error: (message, statusCode) => { failed = true; console.error(new pdfcrowd.Pdfcrowd.Error( message, statusCode).toString()); }, end: () => { if (failed) { return; } const html_bytes = Buffer.concat(chunks); // Buffer console.log("HTML ready:", html_bytes.length, "bytes"); } });
Write to a stream
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const { pipeline } = require('node:stream'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); let output_stream; let failed = false; function fail(error) { if (failed) return; failed = true; console.error(error.toString()); process.exitCode = 1; if (output_stream) output_stream.destroy(); } try { const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer client.convertRawData(pdf_data_1, { data: (stream) => { if (failed) { stream.destroy(); return; } output_stream = fs.createWriteStream("logo.html"); pipeline(stream, output_stream, (error) => { if (error) { fail(error); return; } if (failed) return; console.log("Saved:", "logo.html"); }); }, error: (message, statusCode) => fail(new pdfcrowd.Pdfcrowd.Error( message, statusCode)), end: () => { console.log("Conversion response ended."); }, }); } catch (error) { fail(error); }
Read from an input stream
Open logo.pdf as a binary input stream and save the result to logo.html.
const pdfcrowd = require('pdfcrowd'); const fs = require('node:fs'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); const input_stream = fs.createReadStream("logo.pdf"); try { client.convertStreamToFile(input_stream, "logo.html", (error, filePath) => { input_stream.destroy(); if (error) { console.error(error.toString()); process.exitCode = 1; return; } console.log("Saved:", filePath); }); } catch (error) { input_stream.destroy(); throw error; }
Customize the HTML output
These examples upload report.pdf, a document with text, images, and vector graphics.
Convert selected pages
Convert pages 1 and 3 of report.pdf into excerpt.html.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setPrintPageRange("1,3"); client.convertFileToFile("report.pdf", "excerpt.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Export HTML with separate assets
Save the HTML, stylesheets, fonts, and images in report.zip. Extract the archive and serve its HTML with the accompanying files, preserving their relative paths.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setCssMode("separate"); client.setFontMode("separate"); client.setForceZip(true); client.setImageMode("separate"); client.convertFileToFile("report.pdf", "report.zip", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Customize the document preview
Scale pages to 75% and change the background, page spacing, and corners in preview.html. Use !important to override the generated background and margin rules.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setCustomCss( "#page-container { background: #eaf0f6 !important; } .page { margin: 24px auto !important; border-radius: 8px; }" ); client.setScaleFactor(75); client.convertFileToFile("report.pdf", "preview.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Avoid naming conflicts
Prefix generated IDs, CSS classes, and font names with report_ in namespaced.html. This helps avoid naming conflicts when integrating the converted content into another page; adjust the surrounding layout separately.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setHtmlNamespace("report_"); client.convertFileToFile("report.pdf", "namespaced.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Reduce image resolution
Render page graphics at 72 DPI instead of the default 144 DPI and save smaller.html. Lower resolution can reduce file size at the cost of image detail; text remains HTML text.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setDpi(72); client.convertFileToFile("report.pdf", "smaller.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });
Preserve vector graphics with SVG
Use SVG for page graphics in vector.html, preserving vector shapes when zooming. Raster images in the PDF remain raster images within the SVG.
const pdfcrowd = require('pdfcrowd'); const client = new pdfcrowd.PdfToHtmlClient("demo", "demo"); client.setImageFormat("svg"); client.convertFileToFile("report.pdf", "vector.html", (error, filePath) => { if (error) { console.error(error.toString()); return; } console.log("Saved:", filePath); });