How to Convert Byte Array to PDF in Java

Converting a byte array to a PDF is common in many Java applications. This is often necessary when dealing with data stored in a byte array format, such as data received from a database or across a network. Such data can be easily viewed and shared with others by converting it to a PDF.

This article will discuss how to convert a byte array to a PDF in Java and use the IronPDF Java PDF library.

Steps to Convert Byte Array to PDF

  1. Create a Maven Project.
  2. Install IronPDF Library.
  3. Create a byte array containing the data you wish to convert to a PDF.
  4. Convert the byte array to a string using the appropriate character encoding.
  5. Create an HTML string that includes the data from the byte array.
  6. Use IronPDF to render the HTML as a PDF.
  7. Save the PDF to a file.

IronPDF: Java PDF Library

IronPDF Java PDF Library is a popular Java library for generating, reading, and manipulating PDF documents. It is developed by Iron Software, a software development company that specializes in developing productivity libraries and tools.

IronPDF provides a range of features for creating and manipulating PDFs. This includes the ability to:

  1. Convert HTML and plain text documents to PDF, to add text and images to PDFs,
  2. Create and fill PDF forms,
  3. Merge and split PDFs,
  4. and more.

The library also includes support for encryption and digital signatures.

IronPDF uses a simple and intuitive API that makes it easy for developers of all levels to use. Additionally, IronPDF offers comprehensive documentation, code samples, and support to help developers get started with the library.

Let's explore how we can use IronPDF to convert a byte array to a PDF file.

Install IronPDF Java library in Maven Project

To install the IronPDF Java library in a Maven project, follow these steps:

  1. Open your project's pom.xml file and locate the <dependencies> tag.
  2. Add the following XML code to the <dependencies> tag to add IronPDF Java as a dependency:

    <dependency>
       <groupId>com.ironsoftware</groupId>
       <artifactId>com.ironsoftware</artifactId>
       <version>2023.4.4</version>
    </dependency>
    XML
  3. Add the following XML code to the <dependencies> tag to add the SLF4J Simple logger, which IronPDF Java uses to log status messages to the console.

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.3</version>
    </dependency>
    XML
  4. Save the pom.xml file.

After you save the pom.xml file, Maven will automatically download and install the IronPDF Java library and the SLF4J Simple logger. You can then use the IronPDF library in your Java code.

Note that the version number used in the XML code above may not be the latest version available when you install the library. You should always use the latest library version for bug fixes and new features.

Sample Code

Here is the sample code to convert the byte array to a PDF file:

package IronPDF.ironpdf_java;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.ironsoftware.ironpdf.*;

public class test {
    public static void main (String[] args) throws IOException {
        byte[] byteArray = { 84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121 };
        String data = new String(byteArray, StandardCharsets.US_ASCII);
        String HTML = "<h1>" + data + "</h1>";

        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);
        pdf.saveAs("C:\\byteToPdf.pdf");
    }
}
JAVA

Explanation of the Code

Step 1: The provided code demonstrates converting a byte array to a PDF using IronPDF. At the beginning of the code, the necessary Java libraries are imported. This includes the IronPDF library and the StandardCharsets library.

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.ironsoftware.ironpdf.*;
JAVA

Step 2: The first step is to create a byte array containing the data to be converted. In this case, the byte array contains the string "This is byte array" in ASCII format.

byte[] byteArray = { 84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121 };
JAVA

Step 3: Next, the byte array is converted to a string using the US_ASCII character encoding. This is done by calling the String constructor and passing the byte array and the appropriate character encoding as parameters.

String data = new String(byteArray, StandardCharsets.US_ASCII);
JAVA

Step 4: An HTML string is then created using the data from the byte array. The string is wrapped in an H1 tag to make it stand out in the PDF.

String HTML = "<h1>" + data + "</h1>";
JAVA

Step 5: After creating the HTML string, the IronPDF library renders the HTML as a PDF. The PDF is then saved to a file on the local disk with the given filename.

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(HTML);
pdf.saveAs("C:\\byteToPdf.pdf");
JAVA

Output File

Here is the output PDF document generated by IronPDF Java PDF Library:

How to Convert a Byte Array to PDF in Java - Figure 1: Output PDF generated by IronPDF Java

Converting an array of bytes to PDF using IronPDF for Java

Conclusion

By following the steps outlined in this article, you can convert a byte array to a PDF file and save it on your local disk.

IronPDF is a commercial library and requires a license to be used in a production environment. The cost for an IronPDF license starts at $749 per developer per year; there other license options available depending on the number of developers and deployment servers.

However, IronPDF offers a 30-day free trial that allows developers to test the library and evaluate its features and capabilities. This free trial includes full access to all features of the library. Developers can use it to convert up to 20 pages of a PDF document.