React-pdf: Uncaught (in promise) TypeError: Cannot read property 'slice' of undefined

Created on 7 Jul 2020  路  9Comments  路  Source: diegomura/react-pdf

when I try generate blob from my document, then TypeError occured

import { pdf } from "@react-pdf/renderer";
import { saveAs } from "file-saver";

const downloadPdf = async () => {
    const blob = await pdf(<Document />).toBlob();  //error here
    saveAs(blob, "document.pdf");
  };

Most helpful comment

@nicholasstephan you may check this workaround for 2.0

import { saveAs } from "file-saver";
import { pdf } from "@react-pdf/renderer";
import { PDFPage } from "./PDFPage";

const generatePDFDocument = async () => {
  const blobPdf = await pdf(PDFPage());
  blobPdf.updateContainer(PDFPage());
  const result = await blobPdf.toBlob();

  saveAs(result, "document.pdf");
};

export default generatePDFDocument;
<button onClick={generatePDFDocument}>Download PDF</button>

All 9 comments

I was having a similar issue when trying to create a blob.
I fixed it by downgrading
npm i @react-pdf/[email protected]
npm installs by default the version ^2.0.0beta

@m-najera for me this is not working

Same issue here.

@nicholasstephan you may check this workaround for 2.0

import { saveAs } from "file-saver";
import { pdf } from "@react-pdf/renderer";
import { PDFPage } from "./PDFPage";

const generatePDFDocument = async () => {
  const blobPdf = await pdf(PDFPage());
  blobPdf.updateContainer(PDFPage());
  const result = await blobPdf.toBlob();

  saveAs(result, "document.pdf");
};

export default generatePDFDocument;
<button onClick={generatePDFDocument}>Download PDF</button>

Thanks for the heads up @cmnstmntmn, works great!

@cmnstmntmn thanks for the solution man. Works beautifully !!

Thank you, @cmnstmntmn for this amazing solution. I was banging my head to get a workaround this. But I am also anticipating this might break when there's an appropriate fix in the latest beta version.

Thanks @cmnstmntmn
Your solution works great.
I'm closing this issue.

My bad! I removed the beta version as the latest on npm. I'm aware of this bug on 2.0 and will be fixed for next deploy. Thanks for addressing this!

Was this page helpful?
0 / 5 - 0 ratings