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

 

Base64 String To PDF

 

Write the file in one of the local directory using file write operation

 

Base64 String To PDF

 

Deploy and trigger the file, pdf is created in local directory

 

 

Sample application: base64topdf sample application

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JASWANTH GANNINA
JASWANTH GANNINA
5 years ago

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

JASWANTH GANNINA
JASWANTH GANNINA
5 years ago
Reply to  admin

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
      }

Craig Can
Craig Can
5 years ago

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 »

Craig Can
Craig Can
5 years ago
Reply to  admin

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.

Shani Jaiswal
Shani Jaiswal
5 years ago

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)

abhishek
abhishek
4 years ago

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.

Kunal Bramhane
Kunal Bramhane
3 years ago

I am getting an error while opening the PDF

sateesh chandra
sateesh chandra
2 years ago

it helped, started we used Java to convert now it helped us

pavithra
pavithra
1 year ago

Hi If we have array of attachments then how we can proceed