Decode and Encode Base64 | MuleSoft Documentation
# Decode and Encode Base64
The following DataWeave examples show how to convert a file stream into Base64 and to convert a Base64 string into a file stream. They use a PDF file as the input and output.
## Encode a PDF File to Base64
This example performs Base64 encoding on a PDF file. It simulates the transformation in the Read operation from the XML configuration by converting a Base64 file stream (`application/octet-stream`) to Binary and outputting the Binary in the `text/plain` format.
The example uses the following functions:
  * `toBase64` from the `dw::core::Binaries` module to transform a binary value to a Base64 string.
  * `readUrl` from the `Core (dw::Core)` module to input a PDF file.
The example assumes that the PDF file is in the `src/main/resources` directory of a Mule project in Anypoint Studio. It provides the name of the PDF file and specifies the MIME type as `"application/octet-stream"`. Using `"binary"` as the second `readUrl` argument is also valid for this example.


DataWeave Script:
```
%dw 2.0
import * from dw::core::Binaries
var myPDF = readUrl("classpath://pdf-test.pdf", "application/octet-stream")
output text/plain
---
toBase64(myPDF as Binary)
```

Output (partial results):
```
JVBERi0xLjYNJeLjz9MNCjM3IDAgb2JqIDw8L0xpbmVhcml6ZWQgMS9MIDIwNTk3L08gNDAvRSAx
...
```

## Decode Base64
This example simulates the transformation in the Write operation from the XML configuration by converting a Base64 file stream (`application/octet-stream`) to Binary and outputting the Binary in the `application/PDF with binary` format. The example does not generate a PDF file; a Write operation is required to generate such a file.
The examples use the following functions:
  * `fromBase64` in the `dw::core::Binaries` module to transform a Base64 string to a binary value.
  * `readUrl` to input a PDF file.
The example assumes that the PDF file is in the `src/main/resources` directory of a Mule project in Anypoint Studio. The function reads the input PDF as an octet-stream MIME type. Using the MIME type `"binary"` as the second `readUrl` argument is also valid for this example.


DataWeave Script:
```
%dw 2.0
import * from dw::core::Binaries
var myPDF = readUrl("classpath://pdf-test.pdf", "application/octet-stream")
var myPDFasBinary = toBase64(myPDF as Binary)
output application/PDF with binary
---
fromBase64(myPDFasBinary as String) as Binary
```

The output is the text representation of the PDF.
Output (partial results):
```
%PDF-1.6
%����
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj
xref
37 34
0000000016 00000 n
...
```

## XML Configuration Example
In Anypoint Studio, you can create and run a Mule flow that encodes and decodes a PDF file. After you complete the procedure, the example reads the file from the specified local directory using the File Connector Read operation. It writes the PDF to the specified directory by using the File Connector Write operation, and it uses Transform Message components to encode the file to Base64 and to decode that output from Base64.
  1. Create a Mule project in Anypoint Studio:
    1. Select **File > New > Mule Project**.
    2. Enter a name for your Mule project.
    3. Click **Finish**.
  2. Use **Add Module** in the Mule Palette to add the File Connector:
    1. In the **Mule Palette** view, click **(X) Search in Exchange**.
    2. In the **Add Dependencies to Project** window, type `file` in the search field.
    3. Click **File Connector** in Available modules.
    4. Click **Add**.
    5. Click **Finish**.
  3. In the Studio canvas, navigate to the **Configuration XML** tab.
  4. Copy and paste the following example between the `<mule/>` elements of a Mule flow in Anypoint Studio:
```
<http:listener-config name="HTTP_Listener_config"
           doc:name="HTTP Listener config" >
  <http:listener-connection
              host="0.0.0.0"
              port="8081" />
</http:listener-config>
<file:config name="File_Config"
       doc:name="File Config" >
  <file:connection workingDir="/Users/me/Downloads" />
</file:config>
<flow name="encode-decode-base64">
  <http:listener doc:name="Listener"
         path="/transform"
         config-ref="HTTP_Listener_config"/>
  <file:read doc:name="Read"
       config-ref="File_Config"
       path="pdf-test.pdf" />
  <ee:transform doc:name="Transform Message">
    <ee:message>
     <ee:set-payload><![CDATA[%dw 2.0
import * from dw::core::Binaries
output text/plain
---
toBase64(payload as Binary)]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <ee:transform doc:name="Transform Message" >
    <ee:message >
     <ee:set-payload ><![CDATA[%dw 2.0
import * from dw::core::Binaries
output application/pdf with binary
---
fromBase64(payload as String) as Binary]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <file:write doc:name="Write"
       path="/Users/me/pdf-test-result.pdf"/>
  <set-payload
       value='#["PDF file created in the directory specified in the Write operation."]'
       doc:name="Set Payload" />
</flow>
```

Note that it is valid to replace the directive `output application/pdf with binary` in the second Transform Message component with `output application/octet-stream`.
  5. Place a PDF file of your choice into a local directory on your machine.
  6. Correct the input and output directories of the **Read** and **Write** operations:
     * In the **Read** operation, change the **File Path** value `/Users/me/Downloads" to the location of your PDF file.
     * In the **Write** operation, change the **Path** value `/Users/me/pdf-test.pdf"` to the location and file name that you want to output the file.
  7. Run the project in Studio.
  8. Load the `0.0.0.0:8081/transform` into a browser.
This action creates a PDF file in the specified location and generates a separate `transform` file with the message in the Set Payload component, `PDF file created in the directory specified in the Write operation.`


## See Also
  * Binaries (dw::core::Binaries)
  * DataWeave Selectors
  *