How to Remove Extra Blank Spaces with html-to-pdfmake

Set removeExtraBlanks: true in the options object when calling htmlToPdfmake() to strip unwanted whitespace between block-level HTML elements before PDF generation.

The html-to-pdfmake library converts HTML strings into document-definition objects compatible with pdfmake. By default, whitespace characters between block-level tags—such as newlines or spaces between </p> and <p> tags—can render as unexpected blank lines in the final PDF. The removeExtraBlanks option sanitizes the input HTML using regular expressions to eliminate these extra spaces during the conversion process.

Understanding the removeExtraBlanks Option

The removeExtraBlanks flag is a boolean option passed to the converter constructor. According to the source code in index.js, the library stores this flag when instantiating the converter:

this.removeExtraBlanks = (options && typeof options.removeExtraBlanks === "boolean" ? options.removeExtraBlanks : false);

(Source: [index.js](https://github.com/aymkdn/html-to-pdfmake/blob/master/index.js#L47))

By default, this value is false, meaning the library preserves the original whitespace from your HTML string. When enabled, the converter pre-processes the HTML string before DOM parsing occurs.

How the Whitespace Removal Works

When removeExtraBlanks is set to true, the library executes a sanitization routine inside the convertHtml function. This routine targets whitespace that appears between opening and closing tag pairs for block-level elements including <div>, <p>, <h1> through <h6>, <ol>, <ul>, and <li>.

The implementation in index.js uses case-insensitive regular expressions to collapse these spaces:

if (this.removeExtraBlanks)
    htmlText = htmlText
        .replace(/(<\/?(div|p|h1|h2|h3|h4|h5|h6|ol|ul|li)([^>]+)?>)\s+(<\/?(div|p|h1|h2|h3|h4|h5|h6|ol|ul|li))/gi, "$1$4")
        .replace(...);   // pattern repeated for additional cleanup

(Source: [index.js](https://github.com/aymkdn/html-to-pdfmake/blob/master/index.js#L124))

This regex captures whitespace characters (\s+) that sit between specific closing tags and subsequent opening tags, removing them to prevent empty paragraphs or excessive vertical spacing in the generated PDF.

Usage Examples

Basic Whitespace Removal

Without the option enabled, extra line breaks in your HTML source create unwanted blank spaces in the PDF:

const html = `
  <p>First paragraph.</p>
  <p>Second paragraph.</p>
`;

// Default behavior - may show extra blank line between paragraphs
const doc = htmlToPdfmake(html);
pdfMake.createPdf(doc).download();

Enable removeExtraBlanks to clean the HTML before conversion:

const html = `
  <p>First paragraph.</p>
  <p>Second paragraph.</p>
`;

const options = {
  removeExtraBlanks: true
};

const doc = htmlToPdfmake(html, options);
pdfMake.createPdf(doc).download();

Complex Layouts with Tables and Lists

The option also handles stray spaces before tables inside table cells (<td>) or after closing table tags. This is particularly useful when converting richly formatted content:

const html = `
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>

  <table>
    <tr><td>Cell A</td></tr>
    <tr><td>Cell B</td></tr>
  </table>
`;

const doc = htmlToPdfmake(html, { removeExtraBlanks: true });
pdfMake.createPdf(doc).download();

Performance Considerations

According to the project documentation in README.md, enabling removeExtraBlanks can be resource-intensive. The operation runs multiple regular-expression replacements across the entire HTML input string, which may impact performance when processing large documents. Use this option when layout precision is critical, but consider leaving it disabled for simple HTML or when processing speed is a priority.

Summary

  • Set removeExtraBlanks: true in the options object passed to htmlToPdfmake() to eliminate extra blank spaces caused by whitespace between block-level tags.
  • Implementation is found in index.js at lines 47 and 124-125, where the option is stored and regex replacements are executed.
  • Target elements include divs, paragraphs, headings (h1-h6), and list items (ol, ul, li).
  • Performance trade-off: The regex processing adds computational overhead, as noted in the repository README.

Frequently Asked Questions

Does removeExtraBlanks affect spaces inside text content?

No. The removeExtraBlanks option specifically targets whitespace that appears between HTML tags (such as </p> <p>), not spaces within the text content itself. Your inline spacing and text formatting remain intact.

Can I use removeExtraBlanks in the browser version?

Yes. The removeExtraBlanks option is available in both the Node.js implementation (index.js) and the browser build (browser.js). The option handling and whitespace cleaning logic are identical across both environments.

Why am I still seeing blank lines after enabling removeExtraBlanks?

Blank lines may persist if they are caused by empty HTML elements (like <p></p>) rather than whitespace between tags. The option removes space characters between tags, but it does not remove empty elements. Check your HTML source for empty paragraph or div tags that might be generating the extra lines.

Is removeExtraBlanks enabled by default?

No. The default value is false as implemented in index.js line 47. You must explicitly set removeExtraBlanks: true in your options object to enable whitespace cleaning.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →