USING IRONPDF FOR JAVA

How to Convert Byte Array to PDF in Java

Updated September 1, 2024
Share:

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

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 content to PDF and plain text documents to PDF, to add text and images to PDFs,
  2. Create and fill PDF forms with ease,
  3. Merge multiple PDFs and split PDFs into separate documents,
  4. and more.

The library also includes support for encryption to protect your PDFs and digital signatures for document security.

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 to get you started, and support to help developers get started with the library quickly.

Let's explore how to 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 for Java as a dependency:

    <dependency>
       <groupId>com.ironsoftware</groupId>
       <artifactId>com.ironsoftware</artifactId>
       <version>2024.11.4</version>
    </dependency>
    XML
  3. Add the following XML code to the <dependencies> tag to add the SLF4J Simple logger, which IronPDF for 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 for 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 Byte Array to PDF in Java, Figure 1: Output PDF generated by IronPDF Java Output PDF generated by IronPDF 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 purchase for production environments. The cost for an IronPDF license starts at $749 per developer per year; there are other license options available depending on the number of developers and deployment servers.

However, IronPDF offers a free trial of IronPDF 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.

< PREVIOUS
How to Password Protect PDF in Java
NEXT >
How to Split PDF Files in Java

Install with Maven

Version: 2024.11.4

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2024.11.4</version>
</dependency>

Ready to get started? Version: 2024.11 just released

Free Maven Download View Licenses >