How to Create a PDF with Python on a Web Server
Knowledge Base :: PDF Creator Pilot 3 Knowledge Base

See Also Example
Collapse All

Once you have installed PDF Creator Pilot on your system it is available for use from Python.

This sample demonstrates how to use PDF Creator Pilot with Python running under an IIS Web server. You may use any Web server and any library to handle writing to an output stream. Different libraries may provide different ways for writing out data.

You need to import the win32com.client library to be able to work with COM. Then create a new COM object:

import sys
import win32com.client
PDF = win32com.client.Dispatch("PDFCreatorPilot.PDFDocument3")

Set the license data and adjust other properties:

PDF.StartEngine("demo", "demo")
PDF.Compression = 1 # coFlate = 1
PDF.GenerateInMemoryFile = 1

Fill your document with some data:

PDF.BeginDoc()
# draw a message on the current PDF page
PDF.PDFPAGE_SetActiveFont("Verdana", 0, 0, 0, 0, 14, 0)
PDF.PDFPAGE_TextOut(10, 20, 0, "HELLO, from Python!")
PDF.EndDoc()

Then save it to disk,

PDF.SaveToFile("test.pdf", 0)

or send it directly to an output stream:

size = PDF.GetBufferSize()
data = PDF.GetBuffer()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf")
Response.AddHeader("Content-Length", size)
Response.BinaryWrite(data)
Response.End()

Example

Using PDF Creator Pilot in an ASP/Python Page

ASP/Python
[copy to clipboard]
<%@LANGUAGE=Python%>
<%
import sys
import win32com.client

PDF = win32com.client.Dispatch("PDFCreatorPilot.PDFDocument4")
PDF.SetLicenseData("demo", "demo")
PDF.Compression = 1
 
fnt = PDF.AddBuiltInFont(1, 0, 0)
PDF.UseFont(fnt, 14)
PDF.ShowTextAt(30, 30, "Hello from Python!")

## you may save PDF to file
#PDF.SaveToFile("test.pdf", 0)

## or send it directly to user response
size = PDF.GetBufferSize()
data = PDF.GetBuffer()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf")
Response.AddHeader("Content-Length", size)
Response.BinaryWrite(data)
Response.End()
%>

See Also

Reference