Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Using Flows
Answered

PDF Returned from HTTP Response is Blank

(0) ShareShare
ReportReport
Posted on by 51

I have created a flow that acts like a web service. There is an HTTP Request to trigger the flow.  The body of the flow generates a PDF document.  I know that the PDF document is generated successfully because I can both download it from the step where it is created and I can attach it to an email step and view the correct pdf attachment when the email is received.

 

The last step of the flow is an HTTP Response that returns the PDF file, to be consumed by the caller.  However, the PDF file that is returned is blank.  It is not a zero-byte file. Indeed, this blank file has approximately the same number of pages as the correct file that I receive via email.  But all of the pages are empty.

 

I suspect the problem is that the server is not returning the PDF file correctly, although the problem could instead or also be how it is being handled on the client side (which is using ajax/jquery).  

 

I found a few articles that seem to point me in the right direction, that perhaps it's a base64 encoding issue. See, for ex, https://stackoverflow.com/questions/64106168/pdf-is-blank-and-damaged-when-downloading-it-from-api-using-javascript-and-react and https://stackoverflow.com/questions/34436133/pdf-is-blank-when-downloading-using-javascript but in particular:  https://stackoverflow.com/questions/51981473/downloaded-pdf-looks-empty-although-it-contains-some-data:

 

You are experiencing "byte shaving". Bytes consist of 8 bits. For ASCII, only 7 bytes are needed. PDFs are binary files, they need every bit, but some processes treat PDFs as if it were text files, and they remove the highest bit. When that happens, the structure of the PDF consisting of ASCII characters is preserved (which is why it opens in a viewer), but the binary content of the pages is corrupted (which is why the pages are blank).

 

Assuming that is the problem, I have not been able to fix it.  I have taken the body of the PDF file that is generated and attempted to transform it using each of the available base64 expressions (base64, base64ToBinary, base64ToString and decodeToBase64), just to be comprehensive.  I have also set the content-type to application/pdf, but also tried application/octet-stream.

 

Here is my client side code (I show 2 approaches, but the first one typically fails completely with an error).  Note I am using Jquery 1.81.

 

 $.ajax({
 url: '[link to flow request]',
 type: 'POST',
 headers: {'Content-Type': 'application/json'},
 data: JSON.stringify(...),
 success: function(response){ 
 console.log(response);
 
 var a = document.createElement('a');
 a.style = 'display: none';
 
/* 
(1) first approach
 this returns an error: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed 
 var url = window.URL.createObjectURL(response);
*/

/* (2) Second approach*/
 
 var binaryData = [];
 binaryData.push(response);
 var url = window.URL.createObjectURL(new Blob(binaryData, {type: "application/pdf"})); 

// I have also tried application/octet-stream 
 
 a.href = url;
 a.download = outputFileName;
 document.body.append(a);
 a.click();
 a.remove();
 window.URL.revokeObjectURL(url);
 }
});

 

 

Here is a screenshot of the relevant portions of my flow.  The "Outputs" referenced in the Response is the content of the prior step (GetPDFFIle) btw.

flow.png

Any thoughts on how to get this working?

  • Verified answer
    msalamonjl Profile Picture
    51 on at
    Re: PDF Returned from HTTP Response is Blank

    I figured it out from searching the web.  The issue was client side.  Here is the solution using straight JS:

     

    var req = new XMLHttpRequest(); 
    req.responseType = "blob";
    req.open("POST", "[URL]", true);
    req.setRequestHeader('Content-Type', 'application/json');
    req.onreadystatechange = function () {
     if (req.readyState === 4 && req.status === 200) {
     if (typeof window.chrome !== 'undefined') {
     // Chrome version
     var link = document.createElement('a');
     link.href = window.URL.createObjectURL(req.response);
     link.download = outputFileName;
     link.click();
     } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
     // IE version
     var blob = new Blob([req.response], { type: 'application/pdf' });
     window.navigator.msSaveBlob(blob, outputFileName);
     } else {
     // Firefox version
     var file = new File([req.response], outputFileName, { type: 'application/force-download' });
     window.open(URL.createObjectURL(file));
     }
     callback();
     }
    };
    req.send(JSON.stringify([JSON_OBJECT]));

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,522 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,890 Most Valuable Professional

Leaderboard

Featured topics

Restore a deleted flow