Code Sample for txt/2/pdf API

Python


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

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

This sample is also available as a Colab notebook.

# Sample code for accessing the PDFTeam API, endpoint: txt/2/pdf

import requests
import json

url = 'https://api.pdfteam.com/api/txt/2/pdf'

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

headers = {
  'content-type': 'application/json'
}

response = requests.request('POST', url, data=json.dumps(payload), headers=headers)

if response.status_code == 200:
  result = json.loads(response.text)
  print(result)
else:
  print('Error: ' + response.text)


Sample return:

{'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, watermark, header converted from TXT with Python