Ng2-pdf-viewer: Allow to print PDF

Created on 23 Mar 2017  路  24Comments  路  Source: VadimDez/ng2-pdf-viewer

enhancement help wanted

Most helpful comment

When is going to be implementedthis feature?

Best Regards.

All 24 comments

When is going to be implementedthis feature?

Best Regards.

Is this going to be implemented?

hi, can we print the pdf document ? documentation please.

Hi, this is a very important feature.
would you please add this feature??

Any word on implementing this? Or at least giving some guidelines on how to go about printing a file? Edge and its lack of Blob Urls is creating a major blocker.

I got it to print using

const聽blobUrl = window.URL.createObjectURL((this.srcBinary);
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print();

I got it to print using

const聽blobUrl = window.URL.createObjectURL((this.srcBinary);
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print();

I tryed your code but it doesn't print anything...
Did you solve?
I need to print the pdf showed by ng2-pdf-viewer but I don't know how to do...

@VadimDez I need to print the document with the data on top of it , would you please add this feature??

I am using Print.js (https://printjs.crabbly.com/) in order to print to document:

    let binary = '';
    const len = this.documentSrc.byteLength;
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(this.documentSrc[i]);
    }
    printJS({
      printable: btoa(binary),
      type: 'pdf',
      base64: true
    });

Is this going to be implemented?

I got it to print using

const聽blobUrl = window.URL.createObjectURL((this.srcBinary);
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print();

it works for me without any issue

I got it to print using

const聽blobUrl = window.URL.createObjectURL((this.srcBinary);
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = blobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print();

This only works in chrome , failed in Firefox , edge and IE

I think you should close this. There's no way to reliably trigger a print from the plugin itself.

Google drive even works around this by simply opening the document directly in a new tab, and letting the user decide what to do. It doesn't even trigger the print dialog.

PDF.js allows you to print without opening new tab
see https://mozilla.github.io/pdf.js/web/viewer.html

That's probably using a trick with iframes or something, and the outcome is less than ideal because of all the footer and extra stuff the browser throws in there. If you're trying to print a PDF, you probably don't want that stuff present...

It prints it perfectly. Here is a screenshot from Ctrl + P. My above code does the same thing except it doesn't work in IE11

Capture

This is in Safari

Screen Shot 2020-07-31 at 3 24 57 PM

that's because safari treats the document as a webpage instead of PDF. If it helps, the user can disable print headers and footers in Safari browser.

https://discussions.apple.com/thread/3046001

Well that's a non-starter then, because users aren't that well versed in their browser configuration to know how to do that. It creates pain for support.

Their browser is already capable of viewing and printing PDFs cleanly (even IE11, which uses what reader is available, typically Acrobat), which is why something like Google drive, even with a document previewer, chooses to open in a new tab when print is clicked. Bonus also being that on something like iOS, the native document previewer allows for easy sharing, opening in something else etc

Well fair enough.

One of the reasons many people use ng2-pdf-viewer or pdf.js is because companies want to restrict sensitive documents from being printed or stored on the user's computer (although you can hack around and download the PDF via direct url).

So the print option is not a very strong use case for this library.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I might provide a pull request. Printing isn't that difficult - apart from the problems @jpike88 observed in Safari.

@VadimDez I've got a working prototype. It's not perfect - one of my test files is printed on 19 instead of 18 pages, but it's already good enough for most use cases.

I'm only worried about copyright. My implementation is based on Mozilla's print algorithm. I've ported it to TypeScript and simplified it a lot, so according to German law, I'm the copyright owner. But I'm not sure if that's also true by international law. So it might be difficult to publish it under an MIT license.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VadimDez picture VadimDez  路  5Comments

xmran picture xmran  路  4Comments

quentinmachard-niji picture quentinmachard-niji  路  3Comments

RahmatAliMalik5 picture RahmatAliMalik5  路  5Comments

asadullazadeh picture asadullazadeh  路  4Comments