Hopding / react-native-pdf-lib

Library for creating and editing PDFs in React Native.
https://www.npmjs.com/package/react-native-pdf-lib
189 stars 76 forks source link

Attempt to invoke virtual method 'Int android.graphics.Bitmap.getWidth()' on a null object ref #85

Closed MSH786 closed 3 years ago

MSH786 commented 3 years ago

hi, when i run code,

then i get error, attach screenshot

here is my code

import React from 'react'; import type {Node} from 'react'; import { SafeAreaView,TouchableOpacity, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, View, } from 'react-native'; import PDFLib, { PDFDocument, PDFPage } from 'react-native-pdf-lib';

const App=() => {

const generate=async()=>{

const page1 = PDFPage .create() .setMediaBox(200, 200) .drawText('You can add text and rectangles to the PDF!', { x: 5, y: 235, color: '#007386', }) .drawRectangle({ x: 25, y: 25, width: 150, height: 150, color: '#FF99CC', }) .drawRectangle({ x: 75, y: 75, width: 50, height: 50, color: '#99FFCC', });

// Create a PDF page with text and images const jpgPath = "https://commons.wikimedia.org/wiki/File:Sunflower_from_Silesia2.jpg"// Path to a JPG image on the file system... const pngPath = "https://www.freepik.com/free-photos-vectors/png"// Path to a PNG image on the file system... const page2 = PDFPage .create() .setMediaBox(250, 250) .drawText('You can add JPG images too!') .drawImage(jpgPath, 'jpg', { x: 5, y: 125, width: 200, height: 100, }) .drawImage(pngPath, 'png', { x: 5, y: 25, width: 200, height: 100, });

// Create a new PDF in your app's private Documents directory
const docsDir = await PDFLib.getDocumentsDirectory();
const pdfPath = `${docsDir}/sample.pdf`;
PDFDocument
  .create(pdfPath)
  .addPages(page1, page2)
  .write() // Returns a promise that resolves with the PDF's path
  .then(path => {
    console.log('PDF created at: ' + path);
    // Do stuff with your shiny new PDF!
  });

}

return ( <TouchableOpacity onPress={()=>{ generate() }} >

Generate Pdf now
      </TouchableOpacity>

); };

export default App; WhatsApp Image 2021-04-09 at 5 03 06 PM

MSH786 commented 3 years ago

OR this issue facing becuase of images over internet

I am trying with file system after that revert back to you

MSH786 commented 3 years ago

This issue remain with file system

i have used image picker library but remain same

JavierLaguna commented 3 years ago

I have the same issue, any update?

EDIT: @MSH786 worked for me removing file:// from the file system images path, try it!

MSH786 commented 3 years ago

ok i will use check and revert back to

I have the same issue, any update?

EDIT: @MSH786 worked for me removing file:// from the file system images path, try it!

OK, i will check and revert back to you .

Thank You

rlov commented 3 years ago

I have the same issue :c

thekevinbrown commented 3 years ago

What version of React Native are you guys using, and can you prepare a reproduction project so we can easily run locally and reproduce?

thekevinbrown commented 3 years ago

Alright, as I can't reproduce I'll close this issue. If this is still a problem please open up a new issue with a reproduction repository and I'd be happy to have a look.

nishant-BabyG commented 6 months ago

I have the same issue, any update?

EDIT: @MSH786 worked for me removing file:// from the file system images path, try it!

this worked for me for the package 'react-native-image-to-pdf'

Nihalkottakkal commented 4 months ago

I have the same issue, any update? EDIT: @MSH786 worked for me removing file:// from the file system images path, try it!

this worked for me for the package 'react-native-image-to-pdf'

Hi, could you please help me with this issue ??

nishant-BabyG commented 4 months ago

I have the same issue, any update? EDIT: @MSH786 worked for me removing file:// from the file system images path, try it!

this worked for me for the package 'react-native-image-to-pdf'

Hi, could you please help me with this issue ??

if it's ios keep "file://" else remove it for android.