Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message: Invalid PDF structure #7247

Closed
eswarpdf opened this issue Apr 25, 2016 · 20 comments
Closed

Message: Invalid PDF structure #7247

eswarpdf opened this issue Apr 25, 2016 · 20 comments

Comments

@eswarpdf
Copy link

eswarpdf commented Apr 25, 2016

Link to PDF file (or attach file here):
Can't add pdf file here as it is confidential.

Configuration:

  • Web browser and its version: Firefox 38.0.1
  • Operating system and its version: Windows 7
  • PDF.js version: v1.3.91 (build: d1e83b5)
  • Is an extension: No

Steps to reproduce the problem:

  1. Passing blob to viewer.html

What is the expected behavior? (add screenshot)
pdf viewer rendering pdf file successfully.

What went wrong? (add screenshot)
pdfviewerror

Other notes:
I'm using pdf.js viewer in multiple modules. I was able to use viewer in couple of modules successfully. But in one module I'm getting the error. All the pdf files are either scanned files or normal pdf's.

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Apr 25, 2016

PDF.js version: v1.3.91 (build: d1e83b5)

Please check if the issue is present in the latest version of PDF.js, which as of this writing is 1.5.201, by opening the file with http://mozilla.github.io/pdf.js/web/viewer.html. (Use the "Open file" button on the right-hand side of the toolbar.)

If the issue is still present there, you need to provide the PDF file in order for the issue to be valid, see also https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md.

@yurydelendik
Copy link
Contributor

Can't add pdf file here as it is confidential.

Make the non-confidential example for us.

@eswarpdf
Copy link
Author

Thanks for the quick reply. I'm trying use the latest version. And finding the right document to test and share if it fails.
Thank you.

@eswarpdf
Copy link
Author

I tested with 1.5.188 version.

I'm getting this error as well - Error: Invalid XRef stream header. I'm not able to share the pdf file as I'm unable to upload and test in our server.
What I'm doing is this :

  • Convert base64 to uinit8.
  • Create blob from it , type: application/pdf
  • then call - viewer.html?file=' + encodeURIComponent(url);

Please provide your comments.

@yurydelendik
Copy link
Contributor

Without complete example it's hard to tell. So far we can assume you are doing one of the steps above wrong or you have incorrectly encoded base64 pdf data.

@yurydelendik
Copy link
Contributor

I'm not able to share the pdf file as I'm unable to upload and test in our server.

Closing as incomplete based on the statement above. Please provide complete example to reproduce the issue on the computer other than yours to reopen the issue.

@eswarpdf
Copy link
Author

The same code is working in another module. The base64 conversion is done in java and returned.

@yurydelendik
Copy link
Contributor

It works for me if I run the following in the console (of http://mozilla.github.io/pdf.js/web/viewer.html page):

PDFViewerApplication.pdfDocument.getData().then(function (data) { 
  window.open('viewer.html?file=' + encodeURIComponent(URL.createObjectURL(new Blob([data], {type: 'application/pdf'})))); 
});

The difference is that there is no base64 transforms, so if you are doing create blob and url opening steps right the problem is with your base64 encoding and/or decoding.

@eswarpdf
Copy link
Author

eswarpdf commented Apr 26, 2016

  • Base64 conversion is done by standard jar (commons-codec.jar) and decode is done using the below code
    function base64DecToArr(sBase64, nBlocksSize) {
            var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length, nOutLen =  nBlocksSize? Math
                    .ceil((nInLen * 3 + 1 >> 2) / nBlocksSize)
                    * nBlocksSize
                    : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);
            for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
                nMod4 = nInIdx & 3;
                nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
                if (nMod4 === 3 || nInLen - nInIdx === 1) {
                    for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
                        taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
                    }
                    nUint24 = 0;

                }
            }
            return taBytes;
    }


    function b64ToUint6(nChr) {
            return nChr > 64 && nChr < 91 ? nChr - 65
                : nChr > 96 && nChr < 123 ? nChr - 71 : nChr > 47
                    && nChr < 58 ? nChr + 4 : nChr === 43 ? 62
                    : nChr === 47 ? 63 : 0;
        }

@eswarpdf
Copy link
Author

Forgot to mention, Thank you so much for quick responses.

@yurydelendik
Copy link
Contributor

Running the code above in the browser does not produce any output.

@eswarpdf
Copy link
Author

Can you share the code to decode base64 string to uint8array

@yurydelendik
Copy link
Contributor

See https://github.com/mozilla/pdf.js/blob/master/examples/learning/helloworld64.html . Notice that Blob can accept output from atob as well.

@eswarpdf
Copy link
Author

Tried with ;atob' function passing base64 string. Still getting "Invalid PDF Structure". Will try to attach PDF today.

@eswarpdf
Copy link
Author

If I'm getting byte array from java. What is the way to use it with viewer.html. Sample code will help me.

@yurydelendik
Copy link
Contributor

yurydelendik commented Apr 27, 2016

@eswarpdf don't use base64, just return stream of bytes using setContentType, setContentLength and getOutputStream. See for example http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html . base64 is wasteful for a server's and a web browser's resources.

P.S then just pass a servlet's URL to the viewer.html

@eswarpdf
Copy link
Author

My initial approach itself working. There was mismatch is params passed to URL.
Thank you so much Yury, for quick response and patience.

@eswarpdf
Copy link
Author

eswarpdf commented May 4, 2016

viewer.html?file=' + encodeURIComponent(url)'; is not working in IE11 whereas its working in Firefox.

pdf-not-working-in-ie11

@eswarpdf
Copy link
Author

eswarpdf commented May 4, 2016

I've gone through this thread - #3977 and using latest PDF.js version( 1.5.188). The line number 1103 in pdf.js is different in 1.5.188 version.

Is it still an open issue?

@timvandermeij
Copy link
Contributor

Please do not post comment on this issue that are not related to the original reason this issue was opened. Use the mailing list or IRC to get help with custom solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants