I am aware of a similar question: Pdf.js: rendering a pdf file using the base64 source file instead of the url . Codetoffel answered this question, but my question is different in that my PDF file is retrieved through a RESTful call for my web API implementation. Let me explain ...
Firstly, here is the main way to use PDF.JS to open a PDF file at the URL:
PDFJS.getDocument("/api/path/to/my.pdf").then(function (pdf) { pdf.getPage(1).then(function (page) { var scale = 1; var viewport = page.getViewport(scale); var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; page.render({canvasContext: context, viewport: viewport}); }); });
This works fine, but I use Angular and its $resource service to request a PDF through my RESTful Web API. I know that PDF.JS allows me to replace the URL passing as a string in the PDFJS.getDocument method (above) with the DocumentInitParams object that is defined. Using the DocumentInitParams object is as follows:
var docInitParams = { url: "/api/path/to/my.pdf", httpHeaders: getSecurityHeaders(), //as needed withCredentials: true }; PDFJS.getDocument(docInitParams).then(function (pdf) { ... });
This also works, but it works around my Angular $resource , requiring me to build an api url. But this is normal, because PDFJS allows me to directly transfer PDF data, rather than providing a PDF URL as follows:
var myPdf = myService.$getPdf({ Id: 123 }); //It an Angular $resource, so there is a promise to be fulfilled... myPdf.$promise.then(function() { var docInitParams = { data: myPdf }; PDFJS.getDocument(docInitParams).then(function (pdf) { ... }); });
This is the one I canβt work with. I can say that the myService.$gtPdf returns data as blob or as arraybuffer , but it does not work. I tried converting the returned arraybuffer data to a Uint8Array too, but to no avail.
I'm not sure what else to try and really can use a hint.
How to get the data returned from my service for working with PDFJS?
Thanks in advance.
witttness Jun 18 '14 at 14:27 2014-06-18 14:27
source share