Invoking CutePDF from within Python

J

John Henry

Hi all,

I have a need to invoke CutePDF from within a Python program. The
program creates an EXCEL spreadsheet and set the print area and
properties. Then I wish to store the spreadsheet in a PDF file.
xtopdf does not work well (text only). ReportLab is an overkill.
PyPDF can only shuffle PDF pages.

All I need is to say "Print this to CUTEPDF and store as xyz.pdf".

I found that this can be done in VB but I do not know VB,
unfortunately.

Private Sub Print_PDF()

Dim lRetVal As Long
Dim hKey As Long
Dim sValue As String
lRetVal = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Custom PDF
Printer", _
0&, vbNullString, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, _
0&, hKey, lRetVal)
sValue = "C:\Sample.pdf"
RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len
(sValue)
sValue = "1"
RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
(sValue)

Dim worddoc As Word.Document
Set worddoc = wordapp.Documents.Open("C:\Sample.doc")
wordapp.ActivePrinter = "Custom PDF Printer"
wordapp.PrintOut
worddoc.Close

sValue = "0"
RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
(sValue)
RegCloseKey (hKey)

End Sub
 
T

Tim Golden

John said:
I have a need to invoke CutePDF from within a Python program.

All I need is to say "Print this to CUTEPDF and store as xyz.pdf".
Private Sub Print_PDF()

lRetVal = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Custom PDF
Printer", _
0&, vbNullString, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, _
0&, hKey, lRetVal)
sValue = "C:\Sample.pdf"
RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len
(sValue)
sValue = "1"
RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
(sValue)

Dim worddoc As Word.Document
Set worddoc = wordapp.Documents.Open("C:\Sample.doc")
wordapp.ActivePrinter = "Custom PDF Printer"
wordapp.PrintOut
worddoc.Close

sValue = "0"
RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
(sValue)
RegCloseKey (hKey)


Direct translation (untested since I don't have CutePDF installed)

<code>
import win32api
import win32con
import win32com

hKey, ret = win32api.RegCreateKeyEx (
win32con.HKEY_CURRENT_USER,
"Software\Custom PDF Printer",
win32con.KEY_ALL_ACCESS
)
win32api.RegSetValueEx (hKey, "OutputFile", None, win32con.REG_SZ, r"c:\sample.pdf")
win32api.RegSetValueEx (hKey, "BypassSaveAs", None, win32con.REG_SZ, r"1")

word = win32com.client.gencache.EnsureDispatch ("Word.Application")
doc = word.Documents.Open (r"c:\sample.doc")
doc.ActivePrinter = "Custom PDF Printer"
word.Printout ()
doc.Close ()

win32api.RegSetValueEx (hKey, "BypassSaveAs", None, win32con.REG_SZ, r"0")

</code>


FWIW, I usually generate PDFs by printing to a Postscript printer
(some random Apple Laserthingy) and then using Ghostscript to
do the conversion. I'm happy to post if you're interested.

TJG
 
C

cm

Hi John,
All I need is to say "Print this to CUTEPDF and store as xyz.pdf".
I can't answer you question but let me make a suggestion: Try
PdfCreator. It lets you control all the process using an activex
control. It has events to tell you when the jobs has finish, or report
you of eventual errors.

Download it from sf.net, and look into the samples.

Best regards,

Carlos.
 
P

python

Tim,
FWIW, I usually generate PDFs by printing to a Postscript printer (some random Apple Laserthingy) and then using Ghostscript to do the conversion. I'm happy to post if you're interested.

If its not too much bother, I would be interested in learning how you're
interfacing to Python to Ghostscript.

Regards,
Malcolm
 
T

Tim Golden

Tim,


If its not too much bother, I would be interested in learning how you're
interfacing to Python to Ghostscript.

Really boring, I'm afraid. Just popen and an .exe:

http://pastebin.com/m461bf8f2

(There are a few dependencies I haven't shown but I
hope the approach is obvious; happy to explain / supply
details)

TJG
 
J

John Henry

Hi John,> All I need is to say "Print this to CUTEPDF and store as xyz.pdf".

I can't answer you question but let me make a suggestion: Try
PdfCreator. It lets you control all the process using an activex
control. It has events to tell you when the jobs has finish, or report
you of eventual errors.

Download it from sf.net, and look into the samples.

Best regards,

Carlos.

Thank you for your (and others') response. TJG's VB script
translation was correct but the script failed to invoke CutePDF. So,
I downloaded PDFCreator as suggested. I used the create test page
example and it worked right away.

Thanks for the help.
 

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top