Base64 string shows data in ASCII format. It is popular for embedding inline images and other information in HTML webpages or style sheets. In this article, we will be learning how to convert the Base64 string to PDF, JPG and PNG using Java.
Java Base64 to PDF Converter API - Installation
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>20.7</version> </dependency>
So Aspose.PDF for Java API has been configured. Before proceeding for the conversion of a Base64 string, let us take a look at sample data:
Example String for Base64 Conversion
You can download the Base64 string from here.
Preview of the Base64 Example String
Now, let us proceed to the following Base64 string conversion scenarios:
Convert Base64 String to JPG or PNG Image using Java
JPG, PNG, and other kinds of images are sometimes encoded to Base64 string for safe and reliable communication and transfer of data. Moreover, before we explore PDF conversion, we need to learn about the conversion of a Base64 string to JPG or PNG images using Java. Therefore, you need to follow these steps for the conversion:
- Save the data in a TXT file because of a long string
- Read the string value
- Replace the prefixes
- Save Base64 string to JPG or PNG Image
The following code snippet shows how to convert Base64 string to a JPG or PNG image using Java:
Convert Base64 to PDF using Java
We have learned about converting the Base64 to PNG or JPG images. This is actually an intermediate step of converting Base64 to PDF file. Let us move another step further. Once the string is saved as a raster image, you can easily convert it to PDF. You can follow below steps for converting Base64 string to PDF:
- Remove prefixes of Base64 String
- Convert Base64 string to PNG or JPG image
- Then convert output image to PDF
So the code snippet below shows how to convert Base64 String to PDF using Java language:
To sum up, we have explored how to convert the Base64 string to PDF, PNG, and JPG images. using Java Moreover, generating a PDF file will open up a lot more options for further conversion. Like a PDF file can be converted to SVG, XPS, DOCX, and many other formats. Interested to explore other possibilities? Do not hesitate to discuss this with us via Free Support Forums.