question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import pdf?

See original GitHub issue

I am very new to modern javascript so I am unable to import a pdf file for modification.

My folder structure is like this:

 pdf-lib
  |- package.json
  |- webpack.config.js
  |- /dist
    |- bundle.js
    |- index.html
  |- /src
    |- /assets
      |- account_modification_form.pdf
    |- index.js
  |- /node_modules

I want to add some text to the pdf file in assets folder. Based on this existing issue, I got this code:

fetch('../src/assets/account_modification_form.pdf')
      .then(function(response) {
        response.arrayBuffer()
        .then(function(buffer) {
          const existingPdfDocBytes = new Uint8Array(buffer);
          var pdfDoc = PDFLib.PDFDocumentFactory.load(existingPdfDocBytes);
        })
    });

And my whole index.js file looks like this:

import { PDFDocumentFactory, PDFDocumentWriter, StandardFonts, drawText } from 'pdf-lib';

// This should be a Uint8Array.
// This data can be obtained in a number of different ways.
// If your running in a Node environment, you could use fs.readFile().
// In the browser, you could make a fetch() call and use res.arrayBuffer().

fetch('../src/assets/account_modification_form.pdf')
      .then(function(response) {
        response.arrayBuffer()
        .then(function(buffer) {
          const existingPdfDocBytes = new Uint8Array(buffer);
          var pdfDoc = PDFLib.PDFDocumentFactory.load(existingPdfDocBytes);
        })
    });    

// const pdfDoc = PDFDocumentFactory.load(assets.accountModificationPdfBytes);
const [helveticaRef, helveticaFont] = pdfDoc.embedStandardFont(
  StandardFonts.Helvetica,
);

const pages = pdfDoc.getPages();
const page  = pages[0];

page.addFontDictionary('Helvetica', helveticaRef);

const contentStream = pdfDoc.createContentStream(
  drawText(helveticaFont.encodeText('This text was added to the PDF with JavaScript!'), {
    x: 25,
    y: 25,
    size: 24,
    font: 'Helvetica',
    colorRgb: [0.95, 0.26, 0.21],
  }),
);

page.addContentStreams(pdfDoc.register(contentStream));

const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);

I am getting some errors in console:

Fetch API cannot load file:///E:/<project-url>/src/assets/account_modification_form.pdf. URL scheme must be "http" or "https" for CORS request.

Uncaught ReferenceError: pdfDoc is not defined
    at Module.<anonymous> (bundle.js:1)
    at r (bundle.js:1)
    at bundle.js:1
    at bundle.js:1

Uncaught (in promise) TypeError: Failed to fetch
    at Module.<anonymous> (bundle.js:1)
    at r (bundle.js:1)
    at bundle.js:1
    at bundle.js:1

How to resolve these? Using Webpack, index.js becomes bundle.js in dist folder and I run index.html

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
gegobytecommented, Jun 26, 2019

@philipjmurphy Thank You.

@Hopding I am currently learning the basics of Express as my goal is to make input boxes and user enters some custom text which goes onto the pdf file. So I thought Express can help me in achieving that?

1reaction
Hopdingcommented, Jun 25, 2019

@philipjmurphy Thanks for helping out with this! Much appreciated 👍

@gegobyte Have you been able to get this working? Or are you still running into trouble?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Import a PDF File Into a Document
1. Open the PDF you will import. · 2. Click the camera icon in the PDF's toolbar. · 3. Right-click any location on...
Read more >
Importing a PDF - YouTube
Do you have a screenplay you want to work on but you only have a PDF ? No problem! Learn how to import...
Read more >
Import PDF - YouTube
Import your PDF drawings and convert them into editable DWG files, no external plug-ins needed. All layers will be recognized and converted.
Read more >
Importing PDFs - Canva Help Center
On the side menu of the homepage, click Projects, and go to your Uploads folder. Drag your file to the page to upload...
Read more >
How to Import & Customize PDF Files in Canva - YouTube
Learn how to import your PDF files into Canva and how to customize old PDF files in the Editor. Joanna will guide you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found