It seems like a duplicate question but I have searched all those questions on stackoverflow and have not yet found a solution.
I followed up on the document and set up an example here: [https://jsfiddle.net/r2tc9ob5/][1]
using the same pdf as in Mozilla’s viewer page (https://mozilla.github.io/pdf.js/web/viewer.html).
<script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
<h1>PDF.js 'Hello, world!' example</h1>
<canvas id="the-canvas"></canvas>
var url = 'https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf';
// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
// Asynchronous download of PDF
var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
console.log('PDF loaded');
// Fetch the first page
var pageNumber = 1;
pdf.getPage(pageNumber).then(function(page) {
console.log('Page loaded');
var scale = 1.5;
var viewport = page.getViewport({scale: scale});
// Prepare canvas using PDF page dimensions
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: context,
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function () {
console.log('Page rendered');
});
});
}, function (reason) {
// PDF loading error
console.error(reason);
});
It’s clearly that in jsfiddle link, the PDF was displayed blurry, not sharp as in Mozilla’s viewer example.
Any solution is appreciated.
Thank you very much
2
Answers
Finally, I got it fixed myself,
After digging into the source code of viewer.js, I find out that Mozilla add some calculation for width heigh of the canvas and there is an additional parameter is
transform
.The solution is in https://jsfiddle.net/f4opuvLm/ You can see the difference with offical example source code provided by Mozilla here: https://jsfiddle.net/r2tc9ob5/
I hope this help someone else!
In order to compare two images you need to ensure both are same scale So here I have captured both Pages as presented by JS (note PDF.js is not presenting a PDF as vectors like say a PDF Conformant Reader) it is showing the pages as images with a text overlay, similar to how a scanned OCR would do, just better source text.
Spot the Difference.
So it would appear the full viewer is using coloured subpixels (hardware aspect ?) for Anti-Alias, whereas the Fiddle is using simpler grey tones.
So IF there is a setting it will likely be described in similar terms.