3

I really hope you will be able to help me out on this one. I am new to pdf.js so for the moment, I am playing around with the pre-built version to see if I can integrate this into my web app.

My problem: I am using tcpdf to generate a pdf file which I would like to visualize using pdf.js without having to save it to a file on the server.

I have a php file (generate_document.php) that I use to generate the pdf. The file ends with the following:

$pdf->Output('test.pdf', 'I');

according to the tcpdf documentation, the second parameter can be used to generate the following formats:

I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local server file with the name given by name.
S: return the document as a string (name is ignored).
FI: equivalent to F + I option
FD: equivalent to F + D option
E: return the document as base64 mime multi-part email attachment (RFC 2045)

Then, I would like to view the pdf using pdf.js without creating a file on the server (= not using 'F' as a second parameter and passing the file name to pdf.js).

So, I thought I could simply create an iframe and call the pdf.js viewer pointing to the php file:

<iframe width="100%" height="100%" src="/pdf.js_folder/web/viewer.html?file=get_document.php"></iframe>

However, this is not working at all....do you have any idea what I am overlooking? Or is this option not available in pdf.js?

I have done some research and I have seen some posts here on converting a base64 stream to a typed array but I do not see how this would be a solution to this problem.

Many thanks for your help!!!

EDIT

@async, thanks for your anwer.

I got it figured out in the meantime, so I thought I'd share my solution with you guys.

1) In my get_document.php, I changed the output statement to convert it directly to base64 using

$pdf_output = base64_encode($pdf->Output('test_file.pdf', 'S'));

2) In viewer.js, I use an XHR to call the get_document.php and put the return in a variable (pdf_from_XHR)

3) Next, I convert what came in from the XHR request using the solution that was already mentioned in a few other posts (e.g. Pdf.js and viewer.js. Pass a stream or blob to the viewer)

pdf_converted = convertDataURIToBinary(pdf_from_XHR)

function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));

for (i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
}
return array;
}

et voilà ;-)

Now i can inject what is coming from that function into the getDocument statement:

PDFJS.getDocument(pdf_converted).then(function (pdf) {
        pdfDocument = pdf;
        var url = URL.createObjectURL(blob);
        PDFView.load(pdfDocument, 1.5)
    })
Community
  • 1
  • 1
Robbie Lensen
  • 41
  • 1
  • 4
  • You mean ? Can you see PDF in the PDF.js just opening generate_document.php? – async5 Jul 21 '14 at 23:47
  • If you published PDF.js viewer files on the same site as generate_document.php, you can open PDF file send with 'I' via ./pdfjs/web/viewer.html?file=/myscripts/generate_document.php – async5 Jul 21 '14 at 23:51

0 Answers0