Document Automation Made Simple

Convert Files to PDF in Node.js: DOCX, HTML, Markdown (and Back)

The conversions Node.js backends actually need. Convert DOCX, HTML, and Markdown to PDF. Turn PDF back into JPG images or editable Word, Excel, and PowerPoint files. Every example has working SDK code and real sample files to download.

Tomas, CEO

Sooner or later, every Node.js backend needs to change a file from one format to another. Maybe you need to turn a DOCX contract into a PDF, print a Markdown changelog, or make a JPG preview of a PDF page.

This guide covers the conversions Node developers search for most:

  • DOCX to PDF
  • HTML to PDF
  • Markdown to PDF
  • PDF to JPG
  • PDF to Word, Excel, and PowerPoint

Each one comes with working code, and you can download the real input and output files at the end.

All examples use the official ConvertAPI Node.js SDK, so there is nothing to install besides one npm package. There is no LibreOffice to set up and no headless Chrome to manage. The same code runs on Windows, Linux, macOS, and serverless platforms like AWS Lambda.

Setup

npm install convertapi --save
const convertapi = require('convertapi')(process.env.CONVERTAPI_TOKEN);

Get your API token from the dashboard and keep it in an environment variable.

Every conversion uses the same call shape: convertapi.convert(toFormat, parameters, fromFormat). The call returns a promise, so async/await works naturally.

Convert DOCX to PDF in Node.js

There is no good pure-JavaScript way to convert Word documents. Packages like docx-pdf and libreoffice-convert are wrappers that need LibreOffice installed on your machine. LibreOffice also renders some things differently from Word, so headers, list numbering, and embedded fonts can shift. Puppeteer cannot open a DOCX file at all.

The API takes a different route and converts the document on the server. The output matches what Word itself produces, with headers, numbering, and fonts all in place:

const result = await convertapi.convert('pdf', {
  File: './files/service-agreement.docx'
}, 'docx');

await result.saveFiles('./output/service-agreement.pdf');

The same call accepts doc files. To get a different output, just change the first argument. That is all it takes to switch between DOCX to PDF, DOCX to PNG, or DOCX to HTML.

Convert HTML to PDF in Node.js

The usual self-hosted answer is Puppeteer, which works but leaves you running and patching your own browsers. The HTML to PDF converter does that work for you and renders the page on the server, laid out for print:

const result = await convertapi.convert('pdf', {
  File: './files/report.html',
  RespectViewport: 'false'
}, 'html');

await result.saveFiles('./output/report.pdf');

You can convert a live web page too: use the web source format with a Url parameter instead of a file. Set RespectViewport to false so the page is laid out for print instead of a 1366px screen viewport.

Convert Markdown to PDF in Node.js

You do not need to go through HTML first. MD to PDF is a direct conversion:

const result = await convertapi.convert('pdf', {
  File: './files/release-notes.md'
}, 'md');

await result.saveFiles('./output/release-notes.pdf');

Headings, tables, code blocks, and horizontal rules all come out styled and ready to print. That makes it handy for changelogs, README exports, and generated reports.

Convert PDF to JPG in Node.js

Now for the reverse direction: rendering PDF pages as images for previews and thumbnails. Use PageRange to pick which pages you get:

const result = await convertapi.convert('jpg', {
  File: './files/slide-deck.pdf',
  PageRange: '1'
}, 'pdf');

await result.saveFiles('./output/slide-deck-page-1.jpg');

Each page becomes its own JPG file. The same pattern drives PDF to PNG and PDF to HTML when you need markup instead of pixels.

Convert PDF to Word, Excel, or PowerPoint in Node.js

Developers keep needing the other direction too: turning a PDF back into an editable Office document. PDF to DOCX, PDF to XLSX, and PDF to PPTX all use the same one-liner, and only the target format changes:

const result = await convertapi.convert('docx', {
  File: './files/service-agreement.pdf'
}, 'pdf');

await result.saveFiles('./output/service-agreement.docx');

Swap 'docx' for 'xlsx' to turn tables into a spreadsheet, or for 'pptx' to turn each page into an editable slide.

Which target fits which kind of PDF? As a rule of thumb:

  • Reports fit Word.
  • Invoices fit Excel.
  • Slide-style decks fit PowerPoint.

Our PDF to Word, Excel, and PowerPoint guide covers this in depth, with before-and-after files.

Chain conversions: build a document pipeline

You can chain conversions together. The classic example converts a DOCX body to PDF, then merges it with a pre-designed cover page:

const body = await convertapi.convert('pdf', {
  File: './files/service-agreement.docx'
}, 'docx');

const merged = await convertapi.convert('merge', {
  Files: ['./files/cover-page.pdf', body.file]
}, 'pdf');

await merged.saveFiles('./output/final-document.pdf');

Note the body.file in the second call. It passes the uploaded result straight into the next step, so nothing gets downloaded and re-uploaded in between. For long chains, conversion workflows run the whole pipeline on the server in a single request.

Try It Yourself: Download the Sample Files

Every file below is a real input or output of the exact code above:

Frequently Asked Questions

Can I convert DOCX to PDF in Node.js without LibreOffice or MS Office?

Yes, and that is the main reason to use an API here. The conversion happens on the server with true Word fidelity, so your Node process needs no local office suite. The code runs unchanged in containers and serverless functions.

Do I need Puppeteer for HTML to PDF?

No. The html to pdf conversion renders the page on the API side. Puppeteer is still a fine choice if you already run that infrastructure. The API trades that upkeep for a per-conversion cost.

How do I convert many files at once?

Each convert() call is independent, so standard concurrency works. Map your file list to promises and await Promise.all(...). For multi-step processing of the same document, chain the calls or use a server-side workflow.

What other formats does this cover?

The same three-line pattern handles 500+ conversions, including XLSX and PPTX to PDF and images to PDF. It also covers PDF compression, merge, encryption, and PDF/A archiving. Pick a converter on the conversions page and the interactive demo writes the Node.js snippet for you.

Conclusion

One npm package covers the whole conversion surface: DOCX, HTML, and Markdown into PDF, PDF pages back out as images, and multi-step pipelines chained in a few lines. Start with the DOCX to PDF for Node.js page. The free trial includes 250 conversions, which is enough to wire up and test every example in this post.


Related converters

Ready to Streamline Your File Conversions?