Convert Figma logo to code with AI

bpampuch logopdfmake

Client/server side PDF printing in pure JavaScript

11,595
2,034
11,595
322

Top Related Projects

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

29,192

Client-side JavaScript PDF generation for everyone.

9,788

A JavaScript PDF generation library for Node and the browser

47,890

PDF Reader in JavaScript

14,643

📄 Create PDF files using React

Quick Overview

PDFMake is a client-side PDF generation library for JavaScript. It allows developers to create PDF documents dynamically in the browser or on Node.js servers, offering a powerful and flexible way to generate complex PDF layouts with tables, images, and custom styling.

Pros

  • Easy to use with a straightforward API
  • Supports both browser and Node.js environments
  • Highly customizable with rich formatting options
  • Active development and community support

Cons

  • Limited support for complex layouts compared to some desktop publishing software
  • Performance can be slow for large documents or complex layouts
  • Limited font support, especially for non-Latin scripts
  • Steeper learning curve for advanced features

Code Examples

  1. Creating a simple PDF document:
const docDefinition = {
  content: [
    'Hello world!',
    'This is PDFMake'
  ]
};

pdfMake.createPdf(docDefinition).download('sample.pdf');
  1. Adding a table to the PDF:
const docDefinition = {
  content: [
    {
      table: {
        body: [
          ['Column 1', 'Column 2', 'Column 3'],
          ['Value 1', 'Value 2', 'Value 3'],
          ['Value 4', 'Value 5', 'Value 6']
        ]
      }
    }
  ]
};

pdfMake.createPdf(docDefinition).download('table.pdf');
  1. Styling text and adding images:
const docDefinition = {
  content: [
    { text: 'Styled text', style: 'header' },
    { image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==', width: 150 }
  ],
  styles: {
    header: {
      fontSize: 18,
      bold: true,
      margin: [0, 0, 0, 10]
    }
  }
};

pdfMake.createPdf(docDefinition).download('styled.pdf');

Getting Started

  1. Install PDFMake via npm:

    npm install pdfmake
    
  2. Include PDFMake in your project:

    import pdfMake from 'pdfmake/build/pdfmake';
    import pdfFonts from 'pdfmake/build/vfs_fonts';
    
    pdfMake.vfs = pdfFonts.pdfMake.vfs;
    
  3. Create a PDF document:

    const docDefinition = {
      content: ['Hello, PDFMake!']
    };
    
    pdfMake.createPdf(docDefinition).download('hello.pdf');
    

Competitor Comparisons

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Pros of SheetJS

  • Supports a wide range of spreadsheet formats (Excel, CSV, ODS, etc.)
  • Handles both reading and writing of spreadsheet files
  • Extensive documentation and community support

Cons of SheetJS

  • Larger file size and potentially slower performance
  • Steeper learning curve due to its comprehensive feature set
  • Limited styling options compared to PDF generation

Code Comparison

SheetJS example:

import * as XLSX from 'xlsx';

const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet([{A: 1, B: 2}]);
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
XLSX.writeFile(workbook, "output.xlsx");

pdfmake example:

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';

pdfMake.vfs = pdfFonts.pdfMake.vfs;
const docDefinition = { content: 'Hello, World!' };
pdfMake.createPdf(docDefinition).download('output.pdf');

Summary

SheetJS excels in handling various spreadsheet formats with extensive functionality, while pdfmake focuses on PDF generation with more styling options. SheetJS offers broader file format support but may have a steeper learning curve, whereas pdfmake provides simpler PDF creation but with limited spreadsheet capabilities.

29,192

Client-side JavaScript PDF generation for everyone.

Pros of jsPDF

  • Lighter weight and more focused on core PDF generation functionality
  • Supports a wider range of browsers, including older versions
  • More flexible for low-level PDF manipulation and custom implementations

Cons of jsPDF

  • Less comprehensive documentation and fewer examples compared to pdfmake
  • Requires more manual setup for complex layouts and styling
  • Limited built-in support for advanced features like tables and headers/footers

Code Comparison

pdfmake:

var docDefinition = {
  content: [
    { text: 'Hello, World!', style: 'header' },
    'This is a simple paragraph.'
  ]
};
pdfMake.createPdf(docDefinition).download('example.pdf');

jsPDF:

var doc = new jsPDF();
doc.setFontSize(22);
doc.text('Hello, World!', 10, 10);
doc.setFontSize(16);
doc.text('This is a simple paragraph.', 10, 20);
doc.save('example.pdf');

Both libraries offer PDF generation capabilities, but pdfmake provides a more declarative approach with built-in styling and layout options, while jsPDF offers lower-level control over PDF creation, requiring more manual configuration for complex documents.

9,788

A JavaScript PDF generation library for Node and the browser

Pros of PDFKit

  • More flexible and customizable, allowing for lower-level control over PDF creation
  • Supports both Node.js and browser environments
  • Extensive documentation and examples available

Cons of PDFKit

  • Steeper learning curve due to its lower-level API
  • Requires more code to achieve complex layouts compared to PDFMake
  • Less built-in support for tables and other advanced structures

Code Comparison

PDFKit example:

const PDFDocument = require('pdfkit');
const doc = new PDFDocument();

doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Hello, world!');
doc.end();

PDFMake example:

const pdfMake = require('pdfmake');
const docDefinition = {
  content: ['Hello, world!']
};

pdfMake.createPdf(docDefinition).download('output.pdf');

PDFKit offers more granular control over PDF creation, while PDFMake provides a higher-level API with built-in support for complex layouts. PDFKit is better suited for projects requiring extensive customization, whereas PDFMake excels in quickly generating structured documents with less code.

47,890

PDF Reader in JavaScript

Pros of pdf.js

  • Renders existing PDF files in web browsers
  • Widely adopted and maintained by Mozilla
  • Supports a broad range of PDF features and specifications

Cons of pdf.js

  • Focused on rendering, not creating PDFs
  • Larger file size and potentially slower performance
  • More complex to implement for simple PDF viewing tasks

Code Comparison

pdf.js (viewing a PDF):

pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport({ scale: scale });
    // Render page on canvas
  });
});

pdfmake (creating a PDF):

var docDefinition = {
  content: [
    'Hello, World!'
  ]
};
pdfMake.createPdf(docDefinition).download('myPDF.pdf');

Summary

pdf.js is ideal for rendering existing PDFs in web browsers, offering broad compatibility and feature support. However, it's more complex and resource-intensive than pdfmake. pdfmake, on the other hand, excels at creating PDFs programmatically with a simpler API, but lacks the ability to render existing PDFs. Choose pdf.js for viewing PDFs in-browser, and pdfmake for generating PDFs from scratch.

14,643

📄 Create PDF files using React

Pros of react-pdf

  • Seamless integration with React applications
  • Supports dynamic content generation and real-time updates
  • More flexible styling options using CSS-in-JS

Cons of react-pdf

  • Steeper learning curve for developers not familiar with React
  • Limited support for complex layouts and advanced PDF features
  • Potentially slower rendering for large documents

Code Comparison

react-pdf:

import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

const MyDocument = () => (
  <Document>
    <Page>
      <View>
        <Text>Hello, World!</Text>
      </View>
    </Page>
  </Document>
);

pdfmake:

var docDefinition = {
  content: [
    'Hello, World!'
  ]
};

pdfMake.createPdf(docDefinition).download();

The code comparison shows that react-pdf uses a React-like component structure, while pdfmake uses a more traditional JavaScript object-based approach. react-pdf's syntax may be more familiar to React developers, but pdfmake's structure is simpler for those without React experience.

Both libraries offer powerful PDF generation capabilities, but they cater to different use cases and developer preferences. react-pdf is better suited for React-based applications with dynamic content, while pdfmake may be more appropriate for simpler, static PDF generation tasks or projects not using React.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

pdfmake Node.js CI GitHub npm Bower Packagist CDNJS

PDF document generation library for server-side and client-side in pure JavaScript.

Check out the playground and examples.

This is unstable master branch for version 0.3.x, for stable use version 0.2.x see branch 0.2 or older version 0.1.x see branch 0.1.

Features

  • line-wrapping,
  • text-alignments (left, right, centered, justified),
  • numbered and bulleted lists,
  • tables and columns
    • auto/fixed/star-sized widths,
    • col-spans and row-spans,
    • headers automatically repeated in case of a page-break,
  • images and vector graphics,
  • convenient styling and style inheritance,
  • page headers and footers:
    • static or dynamic content,
    • access to current page number and page count,
  • background-layer,
  • page dimensions and orientations,
  • margins,
  • custom page breaks,
  • font embedding,
  • support for complex, multi-level (nested) structures,
  • table of contents,
  • helper methods for opening/printing/downloading the generated PDF,
  • setting of PDF metadata (e.g. author, subject).

Documentation

Documentation URL: https://pdfmake.github.io/docs/

Source of documentation: https://github.com/pdfmake/docs Improvements are welcome!

Building from sources

using npm:

git clone https://github.com/bpampuch/pdfmake.git
cd pdfmake
npm install
npm run build

using yarn:

git clone https://github.com/bpampuch/pdfmake.git
cd pdfmake
yarn
yarn run build

License

MIT

Authors

pdfmake is based on a truly amazing library pdfkit (credits to @devongovett).

Thanks to all contributors.

NPM DownloadsLast 30 Days