Harshal Kalavadiya
Harshal Kalavadiya

Reputation: 2436

issue in image to PDF not working in react native

I am trying to generate PDF from image path in react native so i am using below plugin for that

https://www.npmjs.com/package/react-native-image-to-pdf/v/1.2.0

As per above doc i configure all the thing and below is my code

const myAsyncPDFFunction = async () => {
try {
  console.log('Call a');
  let path ='file:///Users/macminiharshalk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB719955/data/Containers/Data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/tmp/606C88B3-5759-4942-A544-1231A0C17532.jpg';
  const options = {
    imagePaths: [path],
    name: 'PDFName',
    maxSize: {
      // optional maximum image dimension - larger images will be resized
      width: 900,
      height: Math.round(
        (Dimensions.get('window').height / Dimensions.get('window').width) *
          900,
      ),
    },
    quality: 0.7, // optional compression paramter
    //  targetPathRN: "/storage/emulated/0/Download/", // only for android version 9 and lower
    //for versions higher than 9 it is stored in (Download/img-to-pdf/)
  };
  console.log("options-->", options);
  const pdf = await RNImageToPdf.createPDFbyImages(options);
  console.log('PDF URIs-->', pdf);
  console.log(pdf.filePath);
} catch (e) {
  console.log(e);
  }
};

When i console log i can able to see pdf path as below

/Users/macminiharshalk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB719955/data/Containers/Data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/Documents/PDFName.pdf

When i console option parameter it is showing as below

{"imagePaths": ["file:///Users/macminiharshalk/Library/Developer/CoreSimulator/Devices/FADDF530-05FD-4A0E-9E61-C6AEDB719955/data/Containers/Data/Application/37B8FE42-B23A-4018-865F-F57670B3411E/tmp/606C88B3-5759-4942-A544-1231A0C17532.jpg"], "maxSize": {"height": 1948, "width": 900}, "name": "PDFName", "quality": 0.7}

But when i open PDF image is not copy it is blank PDF so any idea how can i show image in PDF ?

Upvotes: 0

Views: 1209

Answers (1)

Hoang Minh
Hoang Minh

Reputation: 1

please try const newPath = path.replace('file://', ​​'');

Upvotes: 0

Related Questions