如何提高使用pdf.js打印pdf文档的打印质量?

2024-04-23

问题:

当我使用pdf.js打印PDF文档时,纸上的文字不像直接打印PDF那样很清晰。

如何解决呢?


PDF.js 将 PDF 渲染到 HTML 画布,然后将渲染的图像发送到打印机。要提高发送到打印机的图像质量,您需要提高图像的 DPI 或分辨率。

关于这个问题已经提出了几个错误:

  • https://github.com/mozilla/pdf.js/issues/7094 https://github.com/mozilla/pdf.js/issues/7094
  • https://github.com/mozilla/pdf.js/issues/7041 https://github.com/mozilla/pdf.js/issues/7041
  • https://github.com/mozilla/pdf.js/issues/6498 https://github.com/mozilla/pdf.js/issues/6498
  • https://github.com/mozilla/pdf.js/issues/6241 https://github.com/mozilla/pdf.js/issues/6241

这里是请求请求 https://github.com/mozilla/pdf.js/pull/7487。要应用补丁,请找到beforePrint函数并对viewer.js 进行以下更改。

查看器.js

  // increase to improve quality
  var viewport = pdfPage.getViewport(4);
  // Use the same hack we use for high dpi displays for printing to get
  // better output until bug 811002 is fixed in FF.
  var DPI = 72; // increase to improve quality
  var PRINT_OUTPUT_SCALE = DPI/72; 
  var canvas = document.createElement('canvas');

  // The logical size of the canvas.
  canvas.width = Math.floor(viewport.width * PRINT_OUTPUT_SCALE);
  canvas.height = Math.floor(viewport.height * PRINT_OUTPUT_SCALE);

  // The rendered size of the canvas, relative to the size of canvasWrapper.
  canvas.style.width = '100%';

  CustomStyle.setProp('transform' , canvas, 'scale(1,1)');
  CustomStyle.setProp('transformOrigin' , canvas, '0% 0%');

  var canvasWrapper = document.createElement('div');
  canvasWrapper.style.width = '100%';
  canvasWrapper.style.height = '100%';

  canvasWrapper.appendChild(canvas);
  printContainer.appendChild(canvasWrapper);

要提高质量,请将视口系数增加到更高的值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何提高使用pdf.js打印pdf文档的打印质量? 的相关文章

随机推荐