4

error : null is not an object evaluating '_PDFLib.default.createPDF'

https://github.com/Hopding/react-native-pdf-lib

I use the pdf library, can you tell me why the error occurs?

If you use await(const docsDir = await PDFLib.getDocumentsDirectory();), you get an error message like this: error - Can not use keyword 'await' outside an async function.

If you know how to create a pdf file, please let me know.

    pdfButton = () => {
        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',
          });

//It's like a problem.
          // const docsDir = await PDFLib.getDocumentsDirectory();
          const pdfPath = './data/ex.pdf'; //path to create pdf folder
          PDFDocument
            .create(pdfPath)
            .addPages(page1)
            .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!
      });
2
  • Could you add your imports to the question? Commented Aug 28, 2019 at 6:35
  • import PDFLib, {PDFDocument, PDFPage} from 'react-native-pdf-lib' added. Commented Aug 28, 2019 at 6:38

1 Answer 1

3

The pdfButton function must be an async function

pdfButton = async () => {
    ...
    const docsDir = await PDFLib.getDocumentsDirectory();
    const pdfPath = `${docsDir}/ex.pdf`;
}
Sign up to request clarification or add additional context in comments.

4 Comments

error occurs. -> Properties can only be defined on Objects.
There should be a backquote ` and that error is from where? Check traceback.
How to maintain dynamic data in pdf?
@HAPPY What do you mean? If it not related to this question then create a new one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.