How to Run IronPDF for Java in an Azure Function
- IronPDF for Java only supports Docker deployment.
- Zip Deployment is not supported, since IronPDF requires execution of binaries at runtime.
- Follow the Microsoft Official Guide for Creating Function on Linux Using Custom Image
- For
Choose a programming language
-> selectJava
- Follow the guide until your app is up and running.
- For
Add the IronPDF dependency
- Add this to your pom with the latest
<version>
:
<dependencies> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>2022.xx.x</version> </dependency> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-engine-linux-x64</artifactId> <version>2022.xx.x</version> </dependency> </dependencies>
<dependencies> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>2022.xx.x</version> </dependency> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-engine-linux-x64</artifactId> <version>2022.xx.x</version> </dependency> </dependencies>
XML- Note:
ironpdf-engine-linux-x64
is required to run IronPDF in Docker.
- Add this to your pom with the latest
Add a
RenderPdf
function- Add a new function in
Function.java
- This function will receive a URL and return a rendered PDF.
import com.microsoft.azure.functions.*; import com.ironsoftware.ironpdf.PdfDocument; import java.util.Optional; public class Function { /** * Azure function to render a URL as a PDF and return it. * Triggered by an HTTP request with a URL query string parameter. */ @FunctionName("RenderPdf") public HttpResponseMessage renderPdf( @HttpTrigger( name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) { context.getLogger().info("Java HTTP trigger processed a request. (RenderPdf)"); // Parse query parameter for the URL final String url = request.getQueryParameters().get("url"); // Check if the URL parameter is provided if (url == null) { return request.createResponseBuilder(HttpStatus.BAD_REQUEST) .body("Please pass a url on the query string") .build(); } else { try { context.getLogger().info("IronPDF is attempting to render the URL: " + url); // Render the given URL as a PDF PdfDocument pdfDocument = PdfDocument.renderUrlAsPdf(url); // Convert the PDF document to binary data byte[] content = pdfDocument.getBinaryData(); // Return the PDF as an attachment in the response return request.createResponseBuilder(HttpStatus.OK) .body(content) .header("Content-Disposition", "attachment; filename=ironpdf_result.pdf") .build(); } catch (Exception e) { context.getLogger().severe("Failed to render PDF: " + e.getMessage()); return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) .body("An error occurred while rendering the PDF.") .build(); } } } }
import com.microsoft.azure.functions.*; import com.ironsoftware.ironpdf.PdfDocument; import java.util.Optional; public class Function { /** * Azure function to render a URL as a PDF and return it. * Triggered by an HTTP request with a URL query string parameter. */ @FunctionName("RenderPdf") public HttpResponseMessage renderPdf( @HttpTrigger( name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) { context.getLogger().info("Java HTTP trigger processed a request. (RenderPdf)"); // Parse query parameter for the URL final String url = request.getQueryParameters().get("url"); // Check if the URL parameter is provided if (url == null) { return request.createResponseBuilder(HttpStatus.BAD_REQUEST) .body("Please pass a url on the query string") .build(); } else { try { context.getLogger().info("IronPDF is attempting to render the URL: " + url); // Render the given URL as a PDF PdfDocument pdfDocument = PdfDocument.renderUrlAsPdf(url); // Convert the PDF document to binary data byte[] content = pdfDocument.getBinaryData(); // Return the PDF as an attachment in the response return request.createResponseBuilder(HttpStatus.OK) .body(content) .header("Content-Disposition", "attachment; filename=ironpdf_result.pdf") .build(); } catch (Exception e) { context.getLogger().severe("Failed to render PDF: " + e.getMessage()); return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) .body("An error occurred while rendering the PDF.") .build(); } } } }
JAVA- Add a new function in
Update Dockerfile
- Add the IronPDF Linux required packages. From the example, the base Docker image is
mcr.microsoft.com/azure-functions/java:4-java$JAVA_VERSION-build
, which is based onDebian 11
. So we need to add these packages to the Dockerfile.
RUN apt update \ && apt install -y libgdiplus libxkbcommon-x11-0 libc6 libc6-dev libgtk2.0-0 libnss3 \ libatk-bridge2.0-0 libx11-xcb1 libxcb-dri3-0 libdrm-common libgbm1 \ libasound2 libxrender1 libfontconfig1 libxshmfence1 RUN apt-get install -y xvfb libva-dev libgdiplus
- For other Linux distros please see IronPDF Linux Installation Guide
- Add the IronPDF Linux required packages. From the example, the base Docker image is
Redeploy your function to Azure
- Build & package:
mvn clean package
- Build Docker image:
docker build --tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 .
- Push Docker image:
docker push <DOCKER_ID>/azurefunctionsimage:v1.0.0
- Update Azure function:
az functionapp create --name <APP_NAME> --storage-account <STORAGE_NAME> \ --resource-group AzureFunctionsContainers-rg --plan myPremiumPlan \ --deployment-container-image-name <DOCKER_ID>/azurefunctionsimage:v1.0.0
az functionapp create --name <APP_NAME> --storage-account <STORAGE_NAME> \ --resource-group AzureFunctionsContainers-rg --plan myPremiumPlan \ --deployment-container-image-name <DOCKER_ID>/azurefunctionsimage:v1.0.0
SHELL
- Build & package:
- Enjoy IronPDF
- Trigger function at:
https://<APP_NAME>.azurewebsites.net/api/RenderPdf?url=https://www.google.com
- Note: The first time the function is triggered, it may be slow or fail due to initialization, but after that, it should perform consistently.
- Trigger function at:
Frequently Asked Questions
How can I create a PDF generator in Java using Azure Functions?
You can create a PDF generator in Java using Azure Functions by deploying IronPDF through Docker. This involves using the IronPDF library to handle the PDF generation tasks within an Azure Function environment.
Why is Docker deployment necessary for IronPDF on Azure Functions?
Docker deployment is necessary for IronPDF on Azure Functions because it requires executing binaries at runtime, which is not supported by Zip deployment methods.
What are the steps to add IronPDF dependencies to a Maven project?
To add IronPDF dependencies to a Maven project, include the IronPDF and `ironpdf-engine-linux-x64` libraries in the `pom.xml` file. Ensure you are using the latest version numbers for compatibility.
How does the RenderPdf function work in an Azure Function?
The RenderPdf function in an Azure Function reads a URL from an HTTP request, uses IronPDF's `PdfDocument.renderUrlAsPdf` method to render the URL as a PDF, and returns the PDF as an attachment in the response.
What Linux packages are required in the Dockerfile for IronPDF?
The Dockerfile for IronPDF should include Linux packages such as `libgdiplus`, `libxkbcommon-x11-0`, `libc6`, and `libgtk2.0-0` to ensure proper functionality within a Debian-based image.
What should I expect the first time I trigger the PDF generation function?
The first time you trigger the PDF generation function, it may be slow or fail due to initialization processes. Subsequent executions should perform more consistently.
How can I deploy a Docker image for an Azure Function?
To deploy a Docker image for an Azure Function, build your project with `mvn clean package
`, create the Docker image, push it to a Docker registry, and update the Azure Function to use this new image.
What is the correct URL format to trigger a PDF generation function in Azure?
To trigger the PDF generation function, use the URL format: `https://
Where can I find resources for setting up an Azure Function with a custom Docker image?
For setting up an Azure Function with a custom Docker image, you can refer to the Microsoft Official Guide for Creating an Azure Function on Linux Using a Custom Image.