Base64 String To PDF
In this tutorial I will demonstrate how to convert Base64 String To PDF,To create the pdf we would require the base64 content, to demonstrate I have converted a pdf file to base64 using below url and used the same base64 content to write the pdf in one of the local directory and use file write operation.
https://base64.guru/converter/encode/pdf
Create a project in anypoint studio and configure the listener to trigger the flow
Write the dataweave expression to convert the base64 content to pdf
%dw 2.0 import * from dw::core::Binaries output multipart/form-data --- { parts:{ base64Content:{ headers:{ "Content-Type":"application/pdf" }, content: fromBase64(payload) }, } }
Change the mime type to application/pdf
Write the file in one of the local directory using file write operation
Deploy and trigger the file, pdf is created in local directory
Sample application: base64topdf sample application
I am converting a pdf to base64 an attaching in salesforce. However, salesforce is expecting in blob where as the function toBase64 is sending it as string by default. can you help me convert to blob while sending it
blob is nothing but a binary content, convert the base64 to binary. Below DW will help you.
%dw 2.0
import * from dw::core::Binaries
output application/octet-stream
—
fromBase64(payload as String) as Binary
Hi,
The requirement is to convert the pdf data into base64. not the other way. this is the payload for create single operation in salesforce connector.
{
PathOnClient: payload.fileName,
VersionData: toBase64(read(payload.fileContent, ‘application/octet-stream’)) as Binary,
//VersionData: toBase64(payload.fileContent) as Binary,
Title: payload.fileHeader
}
Hi Admin, In my use case I am exposing a mule endpoint where one of the resource retrieves a PDF and displays on browser (not write as a file locally), So I’ve used the above approach of converting base64 data to PDF which is working fine and also able to display on browser if it is just a normal listener. But as I have multiple resources I’ve created a RAML according to it and once implementing the entire solution the PDF does not get displayed on browser but the byte form file gets downloaded due to the set payload at… Read more »
Please share the RAML content. Also, please set response type as application/pdf in RAML and payload data in flow to application/pdf.
Thanks,
Mulesy Team
Hi Admin,
That’s what was originally set, but it didn’t work.
But after changing the listener mime type to multipart form-data now I am able to display it on the browser.
Thank you.
Thanks for Answering!
Even this simple transformation is also working to get the pdf output like you did in earlier post for creating image file :
%dw 2.0
output application/octet-stream
import * from dw::core::Binaries
—
fromBase64(payload as String)
Hi team , good explanation . I have some views on that, if u answer it will be good for me.
1). If i dont use Write File after Set payload , instead that in set payload i set mimetype as
application/pdf, then this also works, i get pdf downloaded directly in brower. But that downloaded pdf name is randomly assigned as 1.pdf, 2.pdf..i want meaningful name , how i can achieve my download pdf comes what i set name like test.pdf …i tried but no luck. It will be helpful if u know any solution.
I am getting an error while opening the PDF
it helped, started we used Java to convert now it helped us
Hi If we have array of attachments then how we can proceed