Question

I am using pdf.js to view PDF files in my GWT application. I've implemented the viewer exactly as the product of a build operation as described on the readme.

When I use the viewer with a static pdf, this works fine. When I supply the link to a servlet that serves the pdf however, the pdf viewer doesn't load.

Works fine

http://127.0.0.1:8888/pdfjs/web/viewer.html?file=http://127.0.0.1:8888/staticpdf.pdf

Doesn't work

http://127.0.0.1:8888/pdfjs/web/viewer.html?file=http://127.0.0.1:8888/api/getPdf?nodeRef=001

http://127.0.0.1:8888/api/getPdf?nodeRef=001 yields a pdf file. The servlet has always worked.

This won't work, because pdf.js#getDocument proceeds to make a GET call without parameters, while the servlet needs the nodeRef:

GET http://127.0.0.1:8888/api/getPdf?nodeRef
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 0

How would I implement the java servlet and pdf.js to be able to view a PDF file given a certain nodeRef? (only the servlet knows how to turn nodeRef into PDF, I need the path to the PDF to remain hidden)

I've been thinking along the lines of api/getPdf/001, but have no idea how to catch this on the tomcat server, and if that is even possible.

Was it helpful?

Solution

It turns out I was thinking too much within pdf.js I had been tinkering with it for hours, and even this question itself has changed a dozen times because I kept finding new leads.

However, I've now found a simple solution.

  • Instead of accessing my servlet as /getPdf?nodeRef=001, I access it /getPdf/001
  • My servlet mapping is now /getPdf/*

The servlet contains the following new code in doGet:

String nodeRef = request.getPathInfo().substring(1);

This omits the need for basic GET parameters in the url, at least in the format ?a=1&b=2, and works fine to pass a variable to a servlet that returns a PDF file using pdf.js.

EDIT: I have editted my question title to reflect the situation so that those who stumble upon this problem too may find their answer here.

OTHER TIPS

If you don't want to change your server mapping, you should encode your URL (with encodeURIComponent, for example):

http://127.0.0.1:8888/api/getPdf?nodeRef=001 will turn into http%3A%2F%2F127.0.0.1%3A8888%2Fapi%2FgetPdf%3FnodeRef%3D001 and pdf.js will handle it correctly as 'file' parameter value.

Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top