# Problems with converting pdf to base64 string

**URL:** <https://forum.robotframework.org/t/problems-with-converting-pdf-to-base64-string/7811>\
**Category:** Standard Libraries\
**Created:** [16 September 2024 10:12 UTC](https://forum.robotframework.org/t/problems-with-converting-pdf-to-base64-string/7811 "2024-09-16T10:12:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phjfranssen](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/phjfranssen/32/3147_2.png) [@phjfranssen](https://forum.robotframework.org/u/phjfranssen)\
**Post date:** [16 September 2024 10:12 UTC](https://forum.robotframework.org/t/problems-with-converting-pdf-to-base64-string/7811/1 "2024-09-16T10:12:11Z")

</div>

Hi all,

I’m trying to build a keyword which can convert a pdf file to a base 64 string. I’ve managed to get the PDF contents into the base64 encoder. But the evaluate expression is returning an error with all the pdf contects. The code I’m using so far:

\*\*\* Settings \*\*\*  
Library Collections  
Library DateTime  
Library String  
Library JSONLibrary  
Library OperatingSystem  
Library Process  
Library BuiltIn

\*\*\* Keywords \*\*\*  
Convert PDF File to Base64 string with filename  
[Arguments] ${filename}  
${pdf\_file\_path}= Set Variable ${CURDIR}\…\testdata\${filename}  
${formattedfilepath}= Replace String ${pdf\_file\_path} \ /  
${file}= Evaluate open(“${formattedfilepath}”, “rb”).read()  
${encoded\_string}= Evaluate base64.b64encode(${file}) modules=base64  
Log ${encoded\_string}  
RETURN ${encoded\_string}

\*\*\* Test Cases \*\*\*  
TC01 - Base64test  
Convert PDF File to Base64 string with filename Document-31757.pdf

Below is the error RF is returning:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/b/ba479e17b4067b73c964f1088097bace55d5f03d.png)

Many thanks in advance,

Peter

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [16 September 2024 15:08 UTC](https://forum.robotframework.org/t/problems-with-converting-pdf-to-base64-string/7811/2 "2024-09-16T15:08:33Z")

</div>

@phjfranssen What are you trying to accomplish with this conversion? But take a look at [python - What is the best way to convert a pdf file into base64Binary? - Stack Overflow](https://stackoverflow.com/questions/43083955/what-is-the-best-way-to-convert-a-pdf-file-into-base64binary)
