Coder Social home page Coder Social logo

How can we print. about react-pdf-js HOT 11 CLOSED

mikecousins avatar mikecousins commented on May 28, 2024
How can we print.

from react-pdf-js.

Comments (11)

gabteles avatar gabteles commented on May 28, 2024 1

I'm currently using an hidden iframe to do this.

HTML:

<iframe name="my-frame" id="my-frame" title="my-frame" style="display: none"></iframe>

JS:

// In my case, I had only blobData from PDF, but you can ignore this and set fileURL directly if it is not yours.
const file = new Blob([blobData], { type: 'application/pdf' });
const fileURL = window.URL.createObjectURL(file);

// As the URL is dynamic, we must set src here, but if it's not, just leave it direct on the HTML. 
// Also, be careful with React literals. If you do this in a <iframe> defined in JSX, it won't work 
// as React will keep src property undefined.
window.frames["my-frame"].src = fileURL;

// Then print:
window.frames["my-frame"].print();

Hope to help :D


Just as a note, I tried to read canvas content as image and print it; the problem is the resolution. As my canvas was resized, the image quality was so bad. :(

from react-pdf-js.

gapipro avatar gapipro commented on May 28, 2024

I am also wondering on how to print pdf without opening it in new tab.

from react-pdf-js.

NjordTech avatar NjordTech commented on May 28, 2024

@gapipro do you have a solution for download or print?

from react-pdf-js.

gapipro avatar gapipro commented on May 28, 2024

My current way of printing is that I open base64 encoded pdf in new tab.

var pdf = "data:application/pdf;base64,"+base64EncodedPdfContent;
window.open(pdf, '_blank');

This forces browser to render PDF with his viewer and then you can print it out.

from react-pdf-js.

NjordTech avatar NjordTech commented on May 28, 2024

great!! for get base64EncodedPdfContent you need to call the url again right?
first you show the preview..
letter you need to call the url again for get the base64 file and then print. ?

from react-pdf-js.

gapipro avatar gapipro commented on May 28, 2024

Well what you can do is fetch PDF once to gets its binary. Load that into react-pdf-js using content property and then use same content in function written above to show it in new tab. This results only in 1 fetch of PDF but side effect it that full PDF is loaded in JS memory.

from react-pdf-js.

lawreenas avatar lawreenas commented on May 28, 2024

@gapipro do you know if there is a limit to length of base64EncodedPdfContent string? My current pdf is over 2 million characters and does not open..

from react-pdf-js.

lawreenas avatar lawreenas commented on May 28, 2024

Apparently there's a chrome bug with large files: https://bugs.chromium.org/p/chromium/issues/detail?id=69227

from react-pdf-js.

quytang avatar quytang commented on May 28, 2024

@gabteles this doesn't work with file from other domains (cross file sharing resources)

from react-pdf-js.

singleclown avatar singleclown commented on May 28, 2024

@gapipro base64EncodedPdfContent怎么获取

from react-pdf-js.

mikecousins avatar mikecousins commented on May 28, 2024

I think this is a bit outside the bounds of this library.

from react-pdf-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.