How to Create PDF with Python on a Web Server


PDF Creator Pilot:       Home       Download       Features       Tutorials       FAQ       Pricing       News

 

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.PDFDocument4")

Set the license data and adjust other properties:

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

Fill your document with some data:

fnt = PDF.AddBuiltInFont(1, 0, 0)
PDF.UseFont(fnt, 14)
PDF.ShowTextAt(30, 30, "Hello from Python!")

Then send it directly to an output stream.

# get generated PDF as binary image
size = PDF.MemoryFileSize
data = PDF.BinaryImage
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

<%@LANGUAGE=Python%>
<%
import sys
import win32com.client
 
PDF = win32com.client.Dispatch("PDFCreatorPilot.PDFDocument3")
PDF.StartEngine("demo", "demo")
PDF.Compression = 1
PDF.GenerateInMemoryFile = 1
 
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()
 
# get generated PDF as binary image
size = PDF.MemoryFileSize
data = PDF.BinaryImage
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()
%>

 


PDF Creator Pilot:       Home       Download       Features       Tutorials       FAQ       Pricing       News