Display PDFs in servoshell using pdf.js viewer#43833
Display PDFs in servoshell using pdf.js viewer#43833Messi002 wants to merge 3 commits intoservo:mainfrom
Conversation
Signed-off-by: Messi002 <[email protected]>
webbeef
left a comment
There was a problem hiding this comment.
If you made changes to pdf.js itself, can you create a fork with a "servo" branch? It's hard to review otherwise.
| "<html><head>\ | ||
| <meta http-equiv=\"refresh\" content=\"0;url={viewer_url}\">\ | ||
| </head><body></body></html>", | ||
| ); |
There was a problem hiding this comment.
That will cause the URL of the pdf document to be replaced, which is not what we want.
There was a problem hiding this comment.
hmm... ok!
I will check how Firefox handles that but I read something like Firefox loads the html viewer directly
| context.protocols.is_fetchable(url.scheme()) | ||
| }, | ||
| Referrer::NoReferrer => false, | ||
| }; |
There was a problem hiding this comment.
that looks dangerous in general...
There was a problem hiding this comment.
Ok! I suppose I have to find another way to give the PDF access without necessarily opening it up to all the fetchable protocols
|
I looked again at how Firefox handles the whole pdf logic, it does it by changing the content type to text/html and then serves the html viewer. added to that, it then passes the pdf data to the viewer through a post message instead of refetching it. so I think we can do same when we detect application/pdf and pass the pdf data to the viewer via javascript
@webbeef what do you think? |
|
The principle looks fine, but I'm not sure how you will "pass the pdf data to the viewer via javascript" . |
|
So the way I will pass it via the javascript is so: Since firefox doesn'tt call PDFViewerApplication.open() directly but instead they use postMessage, the PdfStreamConverter collects the pdf bytes as they come in and once we have the viewer page loaded, we sends them via domWindow.postMessage({pdfjsLoadAction: 'complete', data: pdfBytes}) and not forgetting here that the viewer has a listener that picks up the message and loads the pdf from the raw bytes. For Servo, since we already have the pdf response bytes in the parser, I'm thinking we could load the viewer html as the document content and then inject a small script that sends the same postMessage with the pdf data. That way we're using the same mechanism pdf.js already expects, and we don't need to worry about calling open() before the viewer is ready. |
Adding PDF viewing to servoshell using pdf.js
So when Servo sees that a file is a PDF, it opens the bundled pdf.js viewer and passes the PDF URL to it.
What changed:
Fixes: #38812