PDFObject library: catching error - or how to know when my PDFObject is empty

1.2k views Asked by At

i have a question to everyone use PDFObject library. (http://pdfobject.com/) (I use angularJS in front end)

//This is the html code:
    <div id="boxPdf"></div>

//Here the code in the controller
    var pdfUrl = '/myUrl/myRestService/'+idDocument;
            var myPDF = new PDFObject({
                                    url: pdfUrl,
                                    pdfOpenParams: {
                                        width: "100%",
                                        height: "1500"
                                    },
                                    id: 'myIdObject'
                                });
            myPDF.embed('boxPdf');

The code works, but the annoying thing is that if i send from backend an empty pdf, this library anyway shows the gray box. I'd like to catch this exeption and don't show the gray box for example. How can i look into myPDF and understand if is full or empty? Thanks for the attention and sorry for my bad english, Igor

1

There are 1 answers

1
pipwerks On

Sorry, it is not possible to use JavaScript to determine whether the PDF has loaded.

With Flash, you can determine whether a SWF loaded because Flash Player provides an API that include the PercentLoaded property. This enables developers to query the load status of the SWF.

PDFs do not have a unified 'player', they are typically loaded either by the browser's default PDF rendering engine (usually proprietary), Adobe Reader, or an Adobe competitor like FoxIt Reader. These PDF rendering systems all have completely different capabilities. They have not standardized on any particular feature set or JavaScript API, therefore there is no standard way to check whether the PDF has loaded.

PDFObject was designed to provide fallbacks in case the browser doesn't support PDF embedding. However it cannot verify whether a PDF URL is legitimate or not -- all PDFObject does is create an <object> in your markup, with a src pointing to your URL. It cannot verify whether the URL points to a PDF or any other file type. If you invoke PDFObject and the browser support PDF embedding, you will get an <object> whether your URL is legitimate or not.