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.

  1. Follow the Microsoft Official Guide for Creating Function on Linux Using Custom Image
    • For Choose a programming language -> select Java
    • Follow the guide until your app is up and running.
  2. 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.
  3. 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
  4. 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 on Debian 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
  5. Redeploy your function to Azure

    1. Build & package: mvn clean package
    2. Build Docker image: docker build --tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 .
    3. Push Docker image: docker push <DOCKER_ID>/azurefunctionsimage:v1.0.0
    4. 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
  6. 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.

Frequently Asked Questions

What is the primary deployment method for creating a PDF generator in Java on Azure?

IronPDF for Java only supports Docker deployment on Azure Functions. Zip deployment is not supported due to the need for executing binaries at runtime.

How do I add the necessary dependency in my Java project?

To add IronPDF to your Java project, include the IronPDF and ironpdf-engine-linux-x64 dependencies in your pom.xml with the latest version numbers.

What does the RenderPdf function do in an Azure Function?

The RenderPdf function in the Azure Function takes a URL from the HTTP request and renders it as a PDF document, which is then returned as an attachment in the response.

What packages need to be added to the Dockerfile for PDF generation?

The Dockerfile should include packages like libgdiplus, libxkbcommon-x11-0, libc6, libgtk2.0-0, and others required for IronPDF to run on a Debian-based image.

What should I do if the function is slow or fails the first time it is triggered?

The first time a function is triggered, it might be slow or fail due to initialization processes. After the initial run, it should perform consistently.

How do I deploy my Docker image to Azure?

Build and package your project with 'mvn clean package', build the Docker image, push it to a Docker registry, and then update the Azure function to use the new image.

Where can I find the guide for creating an Azure Function on Linux using a custom image?

You can follow the Microsoft Official Guide for Creating an Azure Function on Linux Using a Custom Image, which provides detailed instructions.

What is the URL format to trigger the RenderPdf function?

The function can be triggered using the URL format: https://.azurewebsites.net/api/RenderPdf?url=

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?