Code sample for html/2/pdf API

PHP


This PHP code demonstrates how to access the PDFTeam html/2/pdf API endpoint. This endpoint loads HTML input (either as a URL or directly as a string) and converts it to PDF. In this case, we convert an HTML invoice to a PDF and add watermarks with some text, a company logo and a QR code. The resulting PDF will be stored in AWS S3, the permalink is then returned to the caller.

The params-array (see below) defines three overlays, one with the text "PAST DUE" in red, the other one with a barcode. The third overlay adds an company logo near the top right of the page:

// Sample PHP code for converting HTML to PDF, endpoint: html/2/pdf
// Overlay #1: Text "PAST DUE"
// Overlay #2: QR barcode
// Overlay #3: Company lofo

<?php
    ...
    $params = array(
        "job" => "ACME INVOICE",
        "user" => "YOUR_USERNAME",
        "key" =>  "YOUR_APIKEY",
        "test" => false,
        "inputOptions" => array(
            "source" => "https://pdfteam.com/invoice.html"
        ),
        "outputOptions" => array(
            "storage" => true,
            "file" => "invoice.pdf"
        ),
        "pdfOptions" => array(
            "format" => "A4",
            "margin" => array(
                "left" => "2cm",
                "top" => "3cm",
                "right" => "2cm",
                "bottom" => "3cm"
            )
        ),
        "overlays" => array(
            array(
                "position" => array(
                  "x" => "left",
                  "y" => "30mm"
                ),
                "type" => "barcode",
                "config" => array(
                  "type" => 19,
                  "data" => "https://pdfteam.com"
                )
            ),
            array(
                "rotate" => 45,
                "position" => array(
                    "x" => "center",
                    "y" => "middle"
                ),
                "type" => "text",
                "config" => array(
                    "data" => "PAST DUE",
                    "font" => "Helvetica Bold",
                    "size" => 72,
                    "color" => "#ff0000",
                    "opacity" => 0.5
                )
            ),
            array(
                "position" => array(
                  "x" => "132mm",
                  "y" => "228mm"
                ),
                "type" => "image",
                "config" => array(
                    "source" => "https://pdfteam.com/samples/acme_logo.png"
                )
            )
        )
    );
    ...
?>


NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark. This is useful for dialing in overlay settings like position or style.


Main PHP script:

// Sample PHP code for accessing the PDFTeam API, endpoint: html/2/pdf
// Sample uses PHP 8.3 with cURL
// Install with sudo apt install php8.3-curl if req'd

<?php
    ...
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => "https://api.pdfteam.com/api/html/2/pdf",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($params),
        CURLOPT_HTTPHEADER => [
            "content-type: application/json",
        ],
    ]);

    $response = curl_exec($curl);
    echo($response);
    curl_close($curl);
    ...
?>


Sample response with link to the converted PDF:

{"endpoint": "html/2/pdf", "job": "ACME INVOICE", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../invoice.pdf"}


Sample result:

PDF Invoice with QR, watermark, logo converted from HTML with PHP