npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

docx-pdf-converter

v1.2.2

Published

A Node.js tool to convert DOCX files to PDF using Mammoth for accurate Word document parsing and Puppeteer for high-quality PDF generation. Ideal for document automation and batch processing.

Downloads

317

Readme

DOCX to PDF Converter

Convert DOCX files to PDF using Mammoth and Puppeteer with preserved formatting and layout.

Installation

npm install docx-pdf-converter

Basic Usage

const fs = require('fs')
const { convertDocxToPdf } = require('docx-pdf-converter')

async function convert() {
  const docxBuffer = fs.readFileSync('path/to/input.docx')
  
  // Return as Buffer (default)
  const result = await convertDocxToPdf(docxBuffer, 'input.docx')
  fs.writeFileSync(result.filename, result.buffer)

  // Return as File
  const fileResult = await convertDocxToPdf(docxBuffer, 'input.docx', { returnType: 'file' })
  console.log(`File saved as ${fileResult.filename}`)

  // Return as Base64
  const base64Result = await convertDocxToPdf(docxBuffer, 'input.docx', { returnType: 'base64' })
  console.log(`Base64 string: ${base64Result.base64.slice(0, 30)}...`)
}

convert()

Advanced Usage with Formatting Options

The converter now supports advanced formatting options to preserve page layout, size, margins, and more:

const fs = require('fs')
const { convertDocxToPdf, extractDocumentMetadata } = require('docx-pdf-converter')

async function convertWithFormatting() {
  const docxBuffer = fs.readFileSync('path/to/input.docx')
  
  // Optional: Analyze document to determine best configuration
  const metadata = await extractDocumentMetadata(docxBuffer)
  console.log('Document analysis:', metadata)
  
  // Basic options (original parameters)
  const options = {
    returnType: 'file', 
    outputDir: './output'
  }
  
  // Formatting options (new parameter)
  const formatOptions = {
    pageConfig: {
      format: 'A4',  // 'A4', 'Letter', 'Legal', etc.
      margin: {
        top: '1in',
        right: '1in',
        bottom: '1in',
        left: '1in'
      }
    },
    preserveHeaders: true  // Attempts to preserve headers and footers
  }
  
  // Convert with formatting options
  const result = await convertDocxToPdf(
    docxBuffer, 
    'input.docx', 
    options, 
    formatOptions
  )
  
  console.log(`File saved as ${result.filename}`)
}

convertWithFormatting()

Smart Conversion (Automatic Document Analysis)

For the best results with minimal configuration, use the smart converter:

const fs = require('fs')
const { smartConvertDocxToPdf } = require('docx-pdf-converter')

async function smartConvert() {
  const docxBuffer = fs.readFileSync('path/to/input.docx')
  
  // Smart converter automatically analyzes the document
  // and applies appropriate formatting
  const result = await smartConvertDocxToPdf(
    docxBuffer,
    'input.docx',
    { returnType: 'file', outputDir: './output' }
  )
  
  console.log(`PDF created at: ${result.filename}`)
}

smartConvert()

API Reference

convertDocxToPdf(fileBuffer, originalFilename, options, formatOptions)

Converts a DOCX file buffer to PDF.

  • fileBuffer (Buffer): DOCX file as a Buffer
  • originalFilename (string): Original filename (used for output naming)
  • options (Object): Basic conversion options
    • returnType (string): 'buffer' (default), 'file', or 'base64'
    • outputDir (string): Output directory for saved files (default: system temp dir)
  • formatOptions (Object): Advanced formatting options (optional)
    • pageConfig (Object): Page configuration
      • format (string): Page format ('A4', 'Letter', 'Legal', etc.)
      • margin (Object): Page margins with top, right, bottom, left properties
    • preserveHeaders (boolean): Whether to preserve headers and footers (default: true)

Returns an object with one or more of:

  • buffer: Buffer of the PDF (if returnType is 'buffer')
  • filename: Name of the output file
  • base64: Base64 string of the PDF (if returnType is 'base64')

extractDocumentMetadata(fileBuffer)

Analyzes a DOCX file to extract metadata useful for formatting decisions.

  • fileBuffer (Buffer): DOCX file as a Buffer

Returns an object with:

  • suggestedFormat: Recommended page format based on content
  • contentLength: Approximate content length
  • hasHeaders: Whether the document appears to have headers
  • hasFooters: Whether the document appears to have footers
  • messages: Any messages from the analysis process

smartConvertDocxToPdf(fileBuffer, originalFilename, options)

Automatically analyzes a document and converts it with optimized formatting.

  • fileBuffer (Buffer): DOCX file as a Buffer
  • originalFilename (string): Original filename (used for output naming)
  • options (Object): Basic conversion options (same as convertDocxToPdf)

Returns the same output as convertDocxToPdf.

Troubleshooting

If you encounter issues with the layout or formatting in the converted PDF:

  1. Try using the smartConvertDocxToPdf function for automatic optimization
  2. Experiment with different page formats and margins
  3. If headers/footers aren't appearing correctly, try setting preserveHeaders: false

Requirements

  • Node.js 12 or higher
  • This package uses Puppeteer which will download a Chrome browser during installation

License

MIT