0.2.1 • Published 2 years ago

react-native-images-to-pdf v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-images-to-pdf

badge

Easily generate PDF files from images in React Native.

Installation

npm install react-native-images-to-pdf

or

yarn add react-native-images-to-pdf

iOS

Run pod install in the ios directory.

Usage

Example using react-native-blob-util

import { createPdf } from 'react-native-images-to-pdf';
import RNBlobUtil from 'react-native-blob-util';

const options = {
  pages: [
    { imagePath: '/path/to/image1.jpg' },
    { imagePath: '/path/to/image2.jpg' }
  ],
  outputPath: `file://${RNBlobUtil.fs.dirs.DocumentDir}/file.pdf`,
};

createPdf(options)
  .then((path) => console.log(`PDF created successfully: ${path}`))
  .catch((error) => console.log(`Failed to create PDF: ${error}`));

This example is using react-native-blob-util to get a valid outputPath, but you can choose any other library to achieve the same functionality.

Example using react-native-document-scanner-plugin

import { createPdf } from 'react-native-images-to-pdf';
import DocumentScanner from 'react-native-document-scanner-plugin';

DocumentScanner.scanDocument()
  .then(({scannedImages}) => {
    if (!scannedImages?.length) {
      throw new Error('No images scanned');
    }

    return createPdf({
      pages: scannedImages.map(imagePath => ({ imagePath })),
      outputPath: `file:///path/to/output/file.pdf`,
    });
  })
  .then(path => console.log(`PDF created successfully: ${path}`))
  .catch(error => console.log(`Failed to create PDF: ${error}`));

API

createPdf(options: CreatePdfOptions) => Promise<string>

Returns a Promise that resolves to a string representing the output path of the generated PDF file.

CreatePdfOptions

PropertyTypeDescription
pagesPage[]Pages that should be included in the PDF.
outputPathstringThe path to the output PDF file.

Valid outputPath

UsageDescriptioniOSAndroid
file:///absolute/path/to/xxx.pdfSave PDF to local file system.

Page

PropertyTypeRequiredDefaultDescription
imagePathstringPath to the image file.
imageFitImageFit'none'Image fitting option. Possible values: 'none', 'fill', 'contain', 'cover'.
widthnumberImage widthWidth of the page in pixels.
heightnumberImage heightHeight of the page in pixels.
backgroundColorstring'white'Background color of the page.

Valid imagePath

UsageDescriptioniOSAndroid
file:///absolute/path/to/image.xxxLoad image from local file system.
data:image/xxx;base64,iVBORw...Load image from base64 string.

Example

Check the example folder for a usage demo.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

0.1.0

2 years ago

0.2.1

2 years ago

0.0.3

2 years ago

0.2.0

2 years ago

0.0.2

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.1

2 years ago