I'm using the test PDF, shown here, which I have saved locally.
I'm using pdfjs-dist, and the pdf.js version 1.8.172 (or higher).
Configuration:
Steps to reproduce the problem:
const url = "http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf";
// other code involved with React
componentDidMount() {
window.PDFJS
.getDocument('../pdf/compressed.tracemonkey-pldi-09.pdf')
.then((pdf) => {
this.renderPdf(pdf);
})
.catch((e) => {
console.log(e);
});
}
renderPdf(pdf) {
const container = document.getElementById("pageContainer");
for (let i = 1; i <= pdf.numPages; i++) {
// Get desired page
pdf.getPage(i).then(function(page) {
const scale = 1.5;
const viewport = page.getViewport(scale);
const div = document.createElement("div");
// Set id attribute with page-#{pdf_page_number} format
div.setAttribute("id", "page-" + (page.pageIndex + 1));
// This will keep positions of child elements as per our needs
div.setAttribute("style", "position: relative");
// Append div within div#container
container.appendChild(div);
// Create a new Canvas element
const canvas = document.createElement("canvas");
// Append Canvas within div#page-#{pdf_page_number}
div.appendChild(canvas);
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: context,
viewport: viewport
};
// Render PDF page
page.render(renderContext);
});
}
What is the expected behavior? The PDF loads
What went wrong? I get a InvalidPDFStructure error.
It also doesn't work for other simple pdfs, for example -->
Any solution @prakashsanker ?
Same problem here - could use an inch?
@ansmonjol @LucasFyl are you sure you have the same problem?
Looking at screenshot it has errors about hex digits String.fromCharCode(108,110,103,61,34,110,34,104)
which is lng="n"
that is part of lang="en"
and it's not part of the PDF (but some HTML code, possibly server error message)
hum, just tried again and it works... It was because I was loading the wrong file as worker with webpackCopyWebpackPlugin
. Fixed now.
This wrong worker was throwing the same issue as @prakashsanker
Oh I was doing something really dumb - I wasn't serving the pdf properly, so I think it was getting parsed some other way.