Pdf-lib: load .pdf from browser

Created on 6 Dec 2018  路  1Comment  路  Source: Hopding/pdf-lib

Hi,
I'm stuck with this explanation :

// This should be a Uint8Array.
// In the browser, you could make a fetch() call and use res.arrayBuffer().

Can someone explain a bit more about this ?
Can this be done with ajax request ?

EDIT
Almost got it with

fetch('docs/example.pdf')
      .then(function(response) {
            response.arrayBuffer().then(function(buffer) {
              const existingPdfDocBytes = buffer[""[[Uint8Array]]""]

But that opens the pdf in the window, don't know why...

Most helpful comment

Hello @mathiasleroy. You snippet looks close to what you want. Try the below code to convert the ArrayBuffer produced by response.arrayBuffer() into a Uint8Array:

response.arrayBuffer()
  .then(function(buffer) {
    const existingPdfDocBytes = new Uint8Array(buffer);
    const pdfDoc = PDFLib.PDFDocumentFactory.load(
      existingPdfDocBytes
    );

>All comments

Hello @mathiasleroy. You snippet looks close to what you want. Try the below code to convert the ArrayBuffer produced by response.arrayBuffer() into a Uint8Array:

response.arrayBuffer()
  .then(function(buffer) {
    const existingPdfDocBytes = new Uint8Array(buffer);
    const pdfDoc = PDFLib.PDFDocumentFactory.load(
      existingPdfDocBytes
    );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinswartz picture kevinswartz  路  8Comments

zimt28 picture zimt28  路  5Comments

AlanHadzic picture AlanHadzic  路  3Comments

DanielJackson-Oslo picture DanielJackson-Oslo  路  3Comments

msvargas picture msvargas  路  6Comments