How to convert Excel to PDF using PHP

Table of contents

    Convert Excel files to PDF using PHP and Nutrient DWS Processor API. This tutorial covers API setup, PHP implementation with cURL, and document processing workflows for automating Excel-to-PDF conversion in your applications.
    How to convert Excel to PDF using PHP
    Summary

    Convert Excel to PDF using Nutrient’s PHP API. Create a free account, get API credentials, and implement conversion using cURL. Combine with 30+ other API tools for merging, watermarking, and page manipulation.

    Convert Excel files to PDFs using Nutrient DWS Processor’s XLSX-to-PDF PHP API. The free plan includes 200 credits. Different operations consume different amounts of credits. Create a free account(opens in a new tab) to get your API key.

    Why convert Excel to PDF?

    Converting Excel files to PDF is essential for workflows requiring document standardization and sharing. Common use cases include:

    • Document preservation — Convert spreadsheets to PDFs to preserve formatting, formulas, and layouts regardless of software or device used to view them.
    • Professional reporting — Generate polished financial reports, invoices, and data summaries that maintain consistent appearance across platforms.
    • Compliance and archiving — Create tamper-resistant records for auditing, regulatory compliance, and long-term document storage.
    • Universal sharing — Share data with stakeholders who don’t have Excel or who prefer read-only formats that prevent accidental editing.
    • Print-ready documents — Ensure spreadsheets print exactly as intended without pagination or formatting issues.

    The Excel-to-PDF API automates this process in your workflow.

    Nutrient DWS Processor API

    Converting Excel-to-PDF is one of 30+ operations available through our PDF API tools. Combine Excel-to-PDF conversion with other tools for complex workflows:

    Your account includes access to all PDF API tools.

    Step 1 — Creating a free account on Nutrient

    Go to our website(opens in a new tab), where you’ll see the page below, prompting you to create your free account.

    Free account Nutrient DWS Processor API

    Once you’ve created your account, you’ll be welcomed by a page showing an overview of your plan details.

    You’ll start with 200 credits to process, and you’ll be able to access all our PDF API tools.

    Step 2 — Obtaining the API key

    After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API keys. You’ll see the following page, which is an overview of your keys.

    Convert Excel-to-PDF PHP API Key

    Copy the Live API key, because you’ll need this for the Excel-to-PDF API.

    Step 3 — Setting up folders and files

    Now, create a folder called excel_to_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside excel_to_pdf and name them input_documents and processed_documents.

    Copy your Excel file to the input_documents folder and rename it to document.xlsx. You can use our demo document as an example.

    Then, in the root folder, excel_to_pdf, create a file called processor.php. This is the file where you’ll keep your code.

    Your folder structure will look like this:

    excel_to_pdf
    ├── input_documents
    | └── document.xlsx
    ├── processed_documents
    └── processor.php

    Step 4 — Writing the code

    Open the processor.php file and paste the code below into it:

    <?php
    $FileHandle = fopen('processed_documents/result.pdf', 'w+');
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.nutrient.io/build',
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_POSTFIELDS => array(
    'instructions' => '{
    "parts": [
    {
    "file": "file"
    }
    ]
    }',
    'file' => new CURLFILE('input_documents/document.xlsx')
    ),
    CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer YOUR_API_KEY_HERE'
    ),
    CURLOPT_FILE => $FileHandle,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    fclose($FileHandle);

    Make sure to replace YOUR_API_KEY_HERE with your API key.

    Code explanation

    The code creates a FileHandle variable to save the file in the processed_documents folder.

    The instructions variable stores all API instructions as a JSON string. The script makes a CURL request to process the conversion.

    Output

    To execute the code, run the command below:

    Terminal window
    php processor.php

    On successful execution, you’ll see a new processed file, result.pdf, located in the processed_documents folder.

    The folder structure will look like this:

    excel_to_pdf
    ├── input_documents
    | └── document.xlsx
    ├── processed_documents
    | └── result.pdf
    └── processor.php

    Additional resources

    Explore more ways to work with Nutrient API:

    Conclusion

    This post covered converting Excel files to PDF documents using the PHP API.

    Integrate these functions into your existing applications. The same API token works for other operations like merging documents, adding watermarks, and more. Sign up(opens in a new tab) for a free trial.

    FAQ

    Do I need a paid account to use Nutrient DWS Processor API?

    No. Start with a free account that provides 200 credits for processing documents. Different operations consume different amounts of credits, so you can process varying numbers of documents depending on the operations you use.

    Does Nutrient DWS Processor API preserve Excel formatting when converting to PDF?

    Yes. Nutrient DWS Processor API maintains spreadsheet formatting, including fonts, colors, cell borders, and merged cells. Complex formulas are calculated and displayed as values in the PDF output.

    Can I convert multi-sheet Excel workbooks to PDF?

    Yes. Nutrient DWS Processor API converts all sheets in an Excel workbook to a single PDF document, with each sheet appearing as a separate page or set of pages depending on the sheet size.

    What PHP extensions are required to use this code?

    The code requires the curl extension for making HTTP requests and the json extension for encoding instructions. Both extensions are typically included in standard PHP installations. Verify with php -m to check installed modules.

    Jonathan D. Rhyne

    Jonathan D. Rhyne

    Co-Founder and CEO

    Jonathan joined PSPDFKit in 2014. As Co-founder and CEO, Jonathan defines the company’s vision and strategic goals, bolsters the team culture, and steers product direction. When he’s not working, he enjoys being a dad, photography, and soccer.

    Explore related topics

    Try for free Ready to get started?