Skip to main content
Bobby_Solo
Inspiring
May 4, 2022
Answered

PDF Services API - File has been damaged

  • May 4, 2022
  • 2 replies
  • 3093 views

After copying the code from Postman I can sucessfully gertate (in PHP using cURL) my tokens. I the POLL the returend link in javascript and sucessfully gernerate a respone. Here is part of the Polling code I use in javescript to save the response.

 

var xhr = new XMLHttpRequest();
xhr.open("GET", '<URL FROM HEADER>', true);
xhr.responseType = "blob";
xhr.setRequestHeader('Authorization', accTok);
xhr.setRequestHeader('x-api-key', '<KEY>');
xhr.onload = function(event) {
    if(this.status == 200) {
        // Create a new Blob object using the response data of the onload object
        var blob = new Blob([this.response], {type: 'application/octetstream'});
        //Create a link element, hide it, direct it towards the blob, and then 'click' it programatically
        let a = document.createElement("a");
        a.style = "display: none";
        document.body.appendChild(a);
        //Create a DOMString representing the blob and point the link element towards it
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = 'myFile.pdf';
        //programatically click the link to trigger the download
        a.click();
        window.URL.revokeObjectURL(url);
        clearInterval(pollInterval);
    }else{
        //deal with your error state here
    }
};
xhr.send();

 

 

This generates and saves the PDF File, however this file can oly be opend in a browser. If it try loading it in Adobe Embed PDF Api, it shows up as damaged. When I try to upload myfile.pdf to this forum I get this:

 

Correct the highlighted errors and try again.

  • The attachment's myfile.pdf content type (application/pdf) does not match its file extension and has been removed.

 

What am I missing?

 

This topic has been closed for replies.
Correct answer Raymond Camden

In this case, you would need to look up how to parse multipart responses - that's not specific to our API, but is generic. I know I struggled with this myself. 

2 replies

Raymond Camden
Community Manager
Community Manager
May 5, 2022

So the issue is that the REST API is *not* returning just PDF binary, but a multipart response that *includes* the PDF. If you open the result in Notepad (or a text editor in general), you will see:

 

--Boundary_1288607_2006293805_1651660181826
Content-Type: application/json
Content-Disposition: form-data; name="contentAnalyzerResponse"

{"cpf:inputs":{"documentIn":{"dc:format":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","cpf:location":"InputFile0"},"params":{"cpf:inline":{"outputFormat":"pdf","jsonDataForMerge":{"author":"Gary Lee","customer":{"name":"Jersy Cargo Logistics","address":{"line1":"4 Florence Alley","line2":"Coolidge Junction","city":"Comanche","state":"Namekagon","zip":98001}},"invoice":{"number":"AFC4S1222203171","date":"09-04-2021, 10:23 AM","customerId":"31LPAC20149E1T","currency":"$","taxPercentage":2.9,"shippingCharge":4.49,"miscellaneous":1.3,"notesSection":"The goods sold are intended for end user consumption and not for resale","orderDetails":{"date":"09-04-2021, 10:20 AM","customerPO":"CPO-12345-09876","orderNo":"OD2100397138762021","salesRep":"Handicrafts","shipping":"Jang Cargo"},"billDetails":[{"quantity":"2","item":"Aviator Sunglass","description":"Light weight, thin and grey color","discount":-25,"total":225},{"quantity":"1","item":"Bike Helmet","description":"ISI approved. Ideal for: Boys, Compact design and strong","discount":0,"total":450}]},"company":{"name":"Projected","address":{"line1":"13H Coastal Road","line2":"Whitestone Bridge","city":"Houston","state":"Texas","zip":12345}},"bankDetails":{"Name":"Safety Center Bank","routingNo":123456789,"accountNo":100200300400}}}}},"cpf:engine":{"repo:assetId":"urn:aaid:cpf:Service-52d5db6097ed436ebb96f13a4c7bf8fb"},"cpf:status":{"completed":true,"type":"","status":200},"cpf:outputs":{"documentOut":{"cpf:location":"multipartLabel","dc:format":"application/pdf"}}}
--Boundary_1288607_2006293805_1651660181826
Content-Type: application/octet-stream
Content-Disposition: form-data; name="multipartLabel"

%PDF-1.7
binary stuff here...

 

In order to get the PDF, you have to handle the mutlpart response first. 

Bobby_Solo
Inspiring
May 6, 2022

Many thanks for the reply. As you can see from my code I'm not hadeling it properly. This is where the online documentation gets thin. I've no idea how to handle the application/octetstream. Is this encoded? How do i extract the pdf exactly in JavaScript? Any help or tips are greatly appreciated. 

Bobby_Solo
Inspiring
May 6, 2022

help! I did not want to mark this as a correct answer. How can i undo this?

Raymond Camden
Community Manager
Community Manager
May 4, 2022

Can you zip the file you got and try attaching it? Or email it to jedimaster@adobe.com.