Code sample for txt/2/pdf API

PHP


This PHP sample demonstrates how to access the PDFTeam txt/2/pdf API endpoint. The code converts a two-page report in plain TXT format to a PDF and adds overlays with some text and a QR code. Also added will be a header that prints the document title on every page. The resulting PDF will be stored in AWS S3, the permalink to the document is returned in the response.

The params-array defines two overlays, one with the text "TOP SECRET" in red, the other one with a barcode. Note the sections near the end, where headers and footers are defined:

// Sample PHP code for accessing the PDFTeam API, endpoint: txt/2/pdf
// Overlay #1: Text "TOP SECRET"
// Overlay #2: QR barcode
// Header: Document title

<?php
    ...
    $params = array(
        "job" => "ACME ANALYTICS",
        "user" => "YOUR_USERNAME",
        "key" =>  "YOUR_APIKEY",
        "test" => false,
        "inputOptions" => array(
            "source" => "https://pdfteam.com/coyote_report.txt"
        ),
        "outputOptions" => array(
            "storage" => true,
            "file" => "acme_analysis.pdf"
        ),
        "pdfOptions" => array(
            "format" => "A4",
            "margin" => array(
                "left" => "2cm",
                "top" => "3cm",
                "right" => "2cm",
                "bottom" => "3cm"
            )
        ),
        "overlays" => array(
            array(
                "position" => array(
                  "x" => "170mm",
                  "y" => "270mm"
                ),
                "type" => "barcode",
                "config" => array(
                  "type" => 19,
                  "data" => "https://pdfteam.com"
                )
            ),
            array(
                "rotate" => 45,
                "position" => array(
                    "x" => "center",
                    "y" => "middle"
                ),
                "type" => "text",
                "config" => array(
                    "data" => "TOP SECRET",
                    "font" => "Helvetica Bold",
                    "size" => 72,
                    "color" => "#ff0000",
                    "opacity" => 0.5
                )
            )
        ),
        "hfOptions" => array(
            "footer" => "&nbsp;",
            "header" =>
            "<div style=\"color:#0000ff;font-size: 12px;margin-left:auto;margin-right:auto;\">TITLE: <span class=\"title\"></span></div>"
        )
    );
    ...
?>


NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark.


Main PHP script:

// Sample PHP code for accessing the PDFTeam API, endpoint: txt/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/txt/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:

{"endpoint": "txt/2/pdf", "job": "ACME ANALYSIS", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../acme_analysis.pdf"}


Sample result:

PDF Report with QR code, watermark, header converted with PDFTEam API from TXT with PHP