Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will discuss how to convert a byte array to a PDF in Java and use the IronPDF Java PDF library.
renderHtmlAsPdf
method to convert the HTML markup to PDFIronPDF 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:
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.
To install the IronPDF Java library in a Maven project, follow these steps:
<dependencies>
tag.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>
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>
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.
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");
}
}
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.*;
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};
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);
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>";
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");
Here is the output PDF document generated by IronPDF Java PDF Library:
Output PDF generated by IronPDF Java
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.
9 .NET API products for your office documents