PDF to HTML Node.js Examples

These examples cover common PDF to HTML tasks in server-side Node.js code. 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.

Importing the client

Examples use ES modules. For CommonJS, replace the import statements with require() and place awaited operations inside an async function.

CommonJS

const pdfcrowd = require('pdfcrowd');

TypeScript

The package includes TypeScript declarations. TypeScript infers conversion results and callback parameters from the SDK types. Use a namespace import:

import * as pdfcrowd from 'pdfcrowd';

For TypeScript compiled as CommonJS, you can also use:

import pdfcrowd = require('pdfcrowd');

When compiling a default import to CommonJS, enable esModuleInterop.

If your project needs Node.js types, see TypeScript dependency setup.

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

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const filePath = await client.convertFileToFile("logo.pdf", "logo.html");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Keep in memory

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const html_bytes = await client.convertFile("logo.pdf"); // Buffer
    console.log("HTML ready:", html_bytes.length, "bytes");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Write to a stream

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const stream = await client.convertFileToStream("logo.pdf");
    try {
        await pipeline(stream, fs.createWriteStream("logo.html"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "logo.html");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Convert a URL

Fetch a PDF from its URL.

Save to a file

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const filePath = await client.convertUrlToFile(
        "https://your-server.com/invoice.pdf", "invoice.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Keep in memory

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const html_bytes = await client.convertUrl(
        "https://your-server.com/invoice.pdf"
    ); // Buffer
    console.log("HTML ready:", html_bytes.length, "bytes");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Write to a stream

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const stream = await client.convertUrlToStream(
        "https://your-server.com/invoice.pdf"
    );
    try {
        await pipeline(stream, fs.createWriteStream("invoice.html"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "invoice.html");
} catch (error) {
    // Handle the error in your application.
    console.error(String(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

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer
    const filePath = await client.convertRawDataToFile(pdf_data_1, "logo.html");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Keep in memory

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer
    const html_bytes = await client.convertRawData(pdf_data_1); // Buffer
    console.log("HTML ready:", html_bytes.length, "bytes");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Write to a stream

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const pdf_data_1 = fs.readFileSync("logo.pdf"); // Buffer
    const stream = await client.convertRawDataToStream(pdf_data_1);
    try {
        await pipeline(stream, fs.createWriteStream("logo.html"));
    } finally {
        stream.destroy();
    }
    console.log("Saved:", "logo.html");
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

Read from an input stream

Open logo.pdf as a binary input stream and save the result to logo.html.

import pdfcrowd from 'pdfcrowd';
import fs from 'node:fs';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    const input_stream = fs.createReadStream("logo.pdf");
    try {
        const filePath = await client.convertStreamToFile(
            input_stream, "logo.html"
        );
        console.log("Saved:", filePath);
    } finally {
        input_stream.destroy();
    }
} catch (error) {
    // Handle the error in your application.
    console.error(String(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.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    client.setPrintPageRange("1,3");
    const filePath = await client.convertFileToFile(
        "report.pdf", "excerpt.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

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.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    client.setCssMode("separate");
    client.setFontMode("separate");
    client.setForceZip(true);
    client.setImageMode("separate");
    const filePath = await client.convertFileToFile("report.pdf", "report.zip");
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

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.

import pdfcrowd from 'pdfcrowd';

try {
    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);
    const filePath = await client.convertFileToFile(
        "report.pdf", "preview.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

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.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    client.setHtmlNamespace("report_");
    const filePath = await client.convertFileToFile(
        "report.pdf", "namespaced.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

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.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    client.setDpi(72);
    const filePath = await client.convertFileToFile(
        "report.pdf", "smaller.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}

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.

import pdfcrowd from 'pdfcrowd';

try {
    const client = new pdfcrowd.PdfToHtmlClient("demo", "demo");
    
    client.setImageFormat("svg");
    const filePath = await client.convertFileToFile(
        "report.pdf", "vector.html"
    );
    console.log("Saved:", filePath);
} catch (error) {
    // Handle the error in your application.
    console.error(String(error));
}