1

For an intranet application, in which very confidential documents are transferred AES256-encrypted, pdf data sources are available as base64-strings (given the fact they are decrypted first). For testing purposes I read a pdf from server http://ddlab.de/stackoverflow/pdf.js/1/test.pdf

Here´s index.html: http://ddlab.de/stackoverflow/pdf.js/1/index.html

In viewer.js I can set

var DEFAULT_URL = 'test.pdf'; // giving a local or crossdomain path to pdf

My idea is, to set a base64 string as default source like

var DEFAULT_URL = 'data:application/pdf;base64,JVBERi0...GCg=='; // shortened

(which also works in FF)

For this I wrote a small php script, that delivers the base64 encoded string. http://ddlab.de/stackoverflow/pdf.js/1/readfile.php

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ob_start('ob_gzhandler');
header('Content-Type: text/javascript; charset=utf-8');
$file = 'test.pdf';
$mime = mime_content_type ($file);
$data = base64_encode(file_get_contents($file));
$url = 'data:' . $mime . ';base64,' . $data;
echo '/*read pdf and output the base64 code*/' . "\n\n";
echo 'var pdf_src = "' . $url . '";';
ob_end_flush();
?>

This is added to HEAD as

<script type="text/javascript" src="readfile.php"></script>

So, finally I can use

var DEFAULT_URL = pdf_src; // retrieving the base64-string as URL

in viewer.js

This works totally great in FF, but in Safari and Chrome ? Nope.

Can somebody help me with this issue ?

ddlab
  • 918
  • 13
  • 28
  • possible duplicate of [Pdf.js: rendering a pdf file using a base64 file source instead of url](http://stackoverflow.com/questions/12092633/pdf-js-rendering-a-pdf-file-using-a-base64-file-source-instead-of-url) – Rob W Mar 27 '14 at 19:14
  • I am receiving an error "Cannot read property 'replace' of undefined", because pdf_src. split ('#') [1] is undefined, even thou pdf_src contains base64 encoded file, can you help me out? – Dime Dec 21 '17 at 10:36

1 Answers1

1

PDFView.open call accepts typed array. Copy "Base64 / binary data / UTF-8 strings utilities" from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding and decode base64 content using base64DecToArr function. You are already modifying the viewer, so adding couple of functions shall not be a problem.

async5
  • 2,505
  • 1
  • 20
  • 27
  • Thanks for your answer. I will get into it the next days and will be back to you. – ddlab Feb 04 '14 at 20:35
  • Hello async5, the function requires a 2nd parameter, what shall I provide as nBlocksSize ? base64DecToArr (sBase64, nBlocksSize). Currently everthing is full off error messages :-) – ddlab Feb 05 '14 at 17:38
  • HAHA, its working ! I just needed to strip the 'data:' . $mime . ';base64,-thing from beginning of base64-code. Thanks for your help, appreciate. Will update the files linked in the question article above. – ddlab Feb 05 '14 at 18:22
  • Thanks again. Nice Nice Nice ! – ddlab Feb 05 '14 at 18:37
  • @ddlab, I have posted the solution that worked for me [here](http://stackoverflow.com/questions/24535799/pdf-js-and-viewer-js-pass-a-stream-or-blob-to-the-viewer/32634740#32634740) – toddmo Sep 17 '15 at 16:00