react-native-html-to-pdf

repository·main·Indexed 19 days ago

https://github.com/christopherdro/react-native-html-to-pdf

A React Native library for converting HTML strings into PDF documents. It provides the generatePDF and convert methods, allowing developers to configure document dimensions, filenames, background colors, and padding. The library supports platform-specific options, such as custom fonts for Android and specific padding controls for iOS, and returns a PDFResult object containing the file path and optional base64 data.

Tokens
1.9K
Snippets
9
Records
14
Agent score
64%

What's inside react-native-html-to-pdf

  1. Reload the application

    main

    If you need to perform a full reload to reset the app state, use the following platform-specific methods:

    • Android: Press the <kbd>R</kbd> key twice or select "Reload" from the Dev Menu (accessed via <kbd>Ctrl</kbd> + <kbd>M</kbd> on Windows/Linux or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> on macOS).
    • iOS: Press <kbd>R</kbd> in the iOS Simulator.
  2. Build and run the iOS app

    main

    For iOS, you must first install CocoaPods dependencies.

    1. If this is your first time setting up the project, install CocoaPods using Ruby bundler:
      bundle install
    2. Every time you update native dependencies, run:
      bundle exec pod install
    3. Once dependencies are installed, run the app:
      # Using npm
      npm run ios
      
      # OR using Yarn
      yarn ios
    # Using npm
    npm run ios
    
    # OR using Yarn
    yarn ios
  3. Use generatePDF to convert HTML to PDF

    main

    Import generatePDF from react-native-html-to-pdf and call it with an options object. The function is asynchronous and returns the results of the PDF generation (such as file paths or base64 data).

    import { generatePDF } from 'react-native-html-to-pdf';
    
    async function createPDF() {
      let options = {
        html: '<h1>PDF TEST</h1>',
        fileName: 'test',
        base64: true,
      };
    
      let results = await generatePDF(options);
      console.log(results);
    }
  4. Configure generatePDF options

    main

    The generatePDF function accepts an options object to control the content and layout of the generated document.

    Common Options

    ParamTypeDefaultNote
    htmlstringHTML string to be converted
    fileNamestringRandomCustom Filename excluding .pdf extension
    base64booleanfalsereturn base64 string of pdf file (not recommended)
    directorystringdefault cache directoryDirectory where the file will be created. On iOS, Documents is the only custom value accepted.
    heightnumber792Set document height (points)
    widthnumber612Set document width (points)
  5. Configure iOS-specific PDF options

    main

    When running on iOS, you can use the following options to control padding and background color:

    ParamTypeDefaultNote
    paddingLeftnumber10Outer left padding (points)
    paddingRightnumber10Outer right padding (points)
    paddingTopnumber10Outer top padding (points)
    paddingBottomnumber10Outer bottom padding (points)
    paddingnumber10Outer padding for any side (points), overrides any padding listed before
    bgColorstring#F6F5F0Background color in Hexadecimal
  6. Generate a PDF with generatePDF()

    main

    Use the generatePDF function to convert HTML content into a PDF file. This function accepts a PDFOptions object and returns a Promise that resolves to a PDFResult object containing the file details.

    import { generatePDF } from 'react-native-html-to-pdf';
    
    const options = {
      // ... PDFOptions properties
    };
    
    const result = await generatePDF(options);
    // result is a PDFResult
  7. Use the convert() method to generate a PDF

    main

    The primary API for this module is the convert method, which takes a PDFOptions object and returns a Promise<PDFResult>. This is the method used to trigger the HTML-to-PDF conversion process.

    import HtmlToPdf from 'react-native-html-to-pdf';
    
    const result = await HtmlToPdf.convert({
      html: '<h1>Hello World</h1>',
      fileName: 'test.pdf',
      base64: true,
    });
    
    console.log(result.filePath);
  8. Handle the PDFResult from convert()

    main

    The convert method returns a Promise that resolves to a PDFResult object. This object contains information about the generated file.

    Fields:

    • filePath: The local file system path to the generated PDF.
    • base64: (Optional) The base64 encoded string of the PDF, available if base64: true was passed in PDFOptions.
    • numberOfPages: (Optional) The total number of pages in the generated PDF.
    export interface PDFResult {
      filePath: string;
      base64?: string;
      numberOfPages?: number;
    }