React-pdf: How to create and print a PDF on the fly without displaying

Created on 20 Dec 2019  路  3Comments  路  Source: diegomura/react-pdf

I am able to create my PDF correctly and have it show up on the screen looking nicely. Now I have a button that I want the user to click that will automatically generate a new PDF and open the print dialog sending that new PDF to the print queue. I'm pretty sure I have to use BlobProvider for this task. But other than that, I'm stuck. I did find another Javascript library called print.js which may be helpful for this task, not sure.

Most helpful comment

Wow! I figured this one out too! (my google-fu is strong today) Hopefully this helps someone else out as well. Functionality appears to be a little wonky in Firefox, but that is an issue with the printjs library. I should be able to figure it out. Works perfect in Chrome though!

const generatePdfDocument = async (numPages) => {
    const blob = await pdf(
        <HelenaPDFPlacard numPages={numPages} />
    ).toBlob();
    const blobURL = URL.createObjectURL(blob);
    printjs(blobURL);
    //saveAs(blob, 'PDFPlacard.pdf');
};

All 3 comments

So I've figured out how to create the pdf and pop open the save/open prompt on the fly. I'd like to take it one step further to create, select the printer and automatically send to the printer.

I looked more at that print.js library and it looks like it will do what I want. It will still have to pop up the print dialog as bypassing that is basically impossible with javascript. But what I'm not sure about is how to automatically save the PDF as a specific filename.

Here is my code so far:

const generatePdfDocument = async (numPages) => {
    const blob = await pdf(
        <HelenaPDFPlacard numPages={numPages} />
    ).toBlob();
    printjs(blob);  // want to do: printjs('PDFPlacard.pdf');
    //saveAs(blob, 'PDFPlacard.pdf');
};

However, the printjs seems to not like the blob. It seems to require the PDF by filename. How can I save the blob as a PDF by name in memory so I can send it to the printjs function?

Wow! I figured this one out too! (my google-fu is strong today) Hopefully this helps someone else out as well. Functionality appears to be a little wonky in Firefox, but that is an issue with the printjs library. I should be able to figure it out. Works perfect in Chrome though!

const generatePdfDocument = async (numPages) => {
    const blob = await pdf(
        <HelenaPDFPlacard numPages={numPages} />
    ).toBlob();
    const blobURL = URL.createObjectURL(blob);
    printjs(blobURL);
    //saveAs(blob, 'PDFPlacard.pdf');
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbrat picture jbrat  路  3Comments

brandly picture brandly  路  3Comments

redcranesolutions picture redcranesolutions  路  4Comments

emt1803 picture emt1803  路  3Comments

benbenedek picture benbenedek  路  3Comments