Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Printing PDF using pdf.js

I am embedding a single page PDF in a page using pdf.js and I want to be able to print just the PDF, not the whole HTML page.

Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had previously loaded a pdf document onto a canvas using pdf.js.

The canvas only contains one page. So This is what worked for me for a single page:

  var canvas = document.getElementById('pdfPage');
  var win = window.open('', '', '');
  var html = "<img src='" + canvas.toDataURL() + "'>";
  win.document.write(html);
  win.document.close();
  win.focus();
  win.print();
  win.close();

I still need to find out what is needed for multiple pages. If I do, I'll edit this answer.

I have to say this approach is not optimal, because it doesn't print the pdf page "camera ready" or in other words in it's original form. It prints an image of the pdf page. The difference is the margins that should not be there and the header / footer that should not be there, as they are not in the original document. Therefore, I'm going to be looking for an approach that prints it like the pdf.js viewer prints it -- in it's original form with fidelity to the orignal document.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...