user3701084
user3701084

Reputation: 11

pdf.js how to pass a file response object in c#

so I am using the excellent pdf.js tool and it works great. However, I'd like to pass it a response object that has the PDF file stream from Amazon S3 over to the viewer of pdf.js.

In the demo I see it calls it like this:

=/pdf/web/viewer.html?file=%2FmypdfFile.pdf

However, looking in viewer.html or pdf.js or any of its files, I cannot see where on earth its using the ?file parameter that is passed on the URL. I'd like to replace it with something where I can pass it a response item and it will load up the viewer.html.

I'd like to do something like this:

(pseudo code sorta)

    request = S3.GetObjectRequest(bucket, key);
    using GetObjectResponse response - client.getobject(request);
    openPDFViewer (response);

Is that doable? response would contain the file, i.e. I can say

response.WriteResponseToFile("c:\mypdf.pdf") 

and I get the file out.

Upvotes: 1

Views: 2212

Answers (2)

miztaken
miztaken

Reputation: 155

I cannot see where on earth its using the ?file parameter that is passed on the URL. I'd like to replace it with something where I can pass it a response item and it will load up the viewer.html.

If you look into viewer.js, there is this pdfViewOpen method having following parameter:- pdfViewOpen(url, id, scale, password,pdfDataRangeTransport, args) The string url that you pass after file= is passed in as url parameter of this method. You can change inside that method what u want to do with it. From there you might want to look at PDFJS.getDocument() method of pdf.js file, this follows to fetchDocument method from where its taken care by messageHandler.

If you want to handle all that by yourself, you can intercept it before. There are already several examples on SO. Here is link to one:- How to get byte-array data from servlet to pdf.js

Upvotes: 1

Michael Adamission
Michael Adamission

Reputation: 493

IIRC S3 files have URL references so you don't have to 'preload' the pdf on the server side. Just render out the URL to have the file=[urlencoded path to s3 file]

Upvotes: 0

Related Questions