pdfmake

repository·master·Indexed 11 days ago

https://github.com/bpampuch/pdfmake

A pure JavaScript library for generating PDF documents on both the client and server side. It uses a declarative approach to define complex structures including tables, columns, nested layouts, vector graphics, and custom page management. Version 0.3.11 supports features such as font embedding, table of contents, and various output formats including Buffer, Base64, and Data URLs.

Tokens
1.3K
Snippets
5
Records
6
Agent score
94%

What's inside pdfmake

  1. Overview of pdfmake features

    master

    pdfmake is a pure JavaScript library for generating PDF documents on both the server-side and client-side. It supports complex document structures and advanced layout features including:

    • Text Layout: Line-wrapping, various text alignments (left, right, centered, justified), and numbered/bulleted lists.
    • Tables and Columns: Supports auto/fixed/star-sized widths, col-spans, row-spans, automatic header repetition on page breaks, and snaking columns (newspaper-style flow).
    • Graphics: Support for images and vector graphics.
    • Styling: Convenient styling with inheritance.
    • Page Management: Page headers and footers (static or dynamic with page numbers), background layers, page dimensions, orientations, margins, and custom page breaks.
    • Advanced Structures: Document sections, font embedding, table of contents, and nested structures.
    • Metadata & Utilities: Setting PDF metadata (author, subject) and helper methods for opening, printing, or downloading the generated PDF.
  2. Run the pdfmake Dev Playground

    master

    The pdfmake-server-playground package provides a development environment for testing pdfmake. To run the playground with automatic server restarts whenever you modify the pdfmake/src directory, use nodemon from the root of the repository.

    Follow these steps from the pdfmake root directory:

    1. Navigate to the playground directory and install dependencies.
    2. Install nodemon globally.
    3. Return to the root and run the playground server using nodemon.
    cd dev-playground
    npm install # or: yarn
    npm install -g nodemon
    cd ..
    nodemon ./dev-playground/server.js
  3. Build pdfmake from source

    master

    If you need to build the project from the source code, you can use either npm or yarn after cloning the repository.

    ### using npm:
    ```bash
    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
  4. Use the pdfmake instance

    master

    The pdfmake module exports a singleton instance of the pdfmake class. This instance inherits from pdfmakeBase and provides the core functionality for generating PDF documents. It also includes internal mechanisms for transforming document definitions into output documents via OutputDocumentServer.

    const pdfmake = require('pdfmake');
    
    // The exported object is a pre-instantiated singleton
    // You can use it directly to generate PDFs
  5. Retrieve PDF data as Buffer, Base64, or Data URL using OutputDocument

    master

    The OutputDocument class provides asynchronous methods to extract the generated PDF content in various formats. It is initialized with a pdfDocumentPromise (a promise that resolves to a readable stream of the PDF).

    Available methods:

    • getStream(): Returns the underlying PDF document promise (the stream).
    • getBuffer(): Returns a Promise<Buffer> containing the entire PDF content.
    • getBase64(): Returns a Promise<string> containing the PDF content encoded in Base64.
    • getDataUrl(): Returns a Promise<string> containing the PDF as a Base64-encoded Data URL (data:application/pdf;base64,...).
    import OutputDocument from 'pdfmake/src/OutputDocument';
    
    // Assuming pdfDocumentPromise is a promise resolving to a readable stream
    const output = new OutputDocument(pdfDocumentPromise);
    
    // Get as Buffer
    const buffer = await output.getBuffer();
    
    // Get as Base64 string
    const base64 = await output.getBase64();
    
    // Get as Data URL for browser usage
    const dataUrl = await output.getDataUrl();
  6. Configure local access policy with setLocalAccessPolicy()

    master

    The setLocalAccessPolicy method allows you to define a security policy for accessing local files. You must provide a callback function that accepts a file path as a string and returns a boolean indicating whether access is permitted.

    If the callback returns true, the file is accessible; otherwise, it is denied.

    const pdfmake = require('pdfmake');
    
    pdfmake.setLocalAccessPolicy((path) => {
      // Only allow files from a specific directory
      return path.startsWith('/safe/directory/');
    });