Jump to content

PDFCreator - Print2PDF


ptrex
 Share

Recommended Posts

PDFCreator in AutoIT

0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII

Someone in the help forum was wondering how to use the PDFCreator in AutoIT.

When reading this, I was surprised to find that PDFCreator had a COM object in it's latest version.

I have been using PDFCreator since years, but never know this :">

Anyhow here is the example on how to create a PDF Test page.

;; Testpage2PDF script
; Part of $PDFCreator
; License: GPL
; Homepage: http://www.sf.net/projects/pdfcreator
; Version: 1.0.0.0
; Date: September, 1. 2005
; Author: Frank Heindörfer
; Comments: Save the test page as pdf-file using the com interface of $PDFCreator.

; Translated by ptrex

AutoItSetOption("MustDeclareVars", 1)

Const $maxTime = 10 ; in seconds
Const $sleepTime = 250 ; in milliseconds

Dim $fso, $WshShell, $PDFCreator, $DefaultPrinter, $ReadyState, $c, _
$Scriptname, $Scriptbasename

$fso = ObjCreate("Scripting.FileSystemObject")

$Scriptbasename = $fso.GetBaseName(@ScriptFullPath)


$WshShell = ObjCreate("WScript.Shell")

$PDFCreator = ObjCreate("PDFCreator.clsPDFCreator")
$PDFCreator.cStart ("/NoProcessingAtStartup")

$ReadyState = 0
With $PDFCreator
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = $fso.GetParentFolderName(@ScriptFullPath)
.cOption("AutosaveFilename") = "Testpage - PDFCreator"
.cOption("AutosaveFormat") = 0 ; 0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII
$DefaultPrinter = .cDefaultprinter
.cDefaultprinter = "PDFCreator"
.cClearcache()
.cPrintPDFCreatorTestpage()
.cPrinterStop = 0
EndWith

$c = 0

Do
$c = $c + 1
    Sleep ($sleepTime)
Until ($ReadyState = 0) and ($c < ($maxTime * 1000 / $sleepTime))

With $PDFCreator
.cDefaultprinter = $DefaultPrinter
    Sleep( 200)
.cClose()
EndWith

If $ReadyState = 0 then
Consolewrite ("Creating test page as pdf." & @CRLF & @CRLF & "An error is occured: Time is up!"& @CR)
ProcessClose("PDFCreator.exe")
EndIf

;--- $PDFCreator events ---

Func PDFCreator_eReady()
$ReadyState = 1
EndFunc

Func PDFCreator_eError()
Consolewrite ("An error is occured!" & @CRLF & @CRLF & _
"Error [" & $PDFCreator.cErrorDetail("Number") & "]: " & $PDFCreator.cErrorDetail("Description")& @CR)
;VA Wscript.Quit
EndFunc

; Convert2PDF script
; Part of $PDFCreator
; License: GPL
; Homepage: http://www.sf.net/projects/pdfcreator
; Version: 1.0.0.0
; Date: September, 1. 2005
; Author: Frank Heindörfer
; Comments: This script convert a printable file in a pdf-file using the com interface of $PDFCreator.

; Translated by ptrex

AutoItSetOption("MustDeclareVars", 1)

Const $maxTime = 30 ; in seconds
Const $sleepTime = 250 ; in milliseconds

Dim $objArgs, $ifname, $fso, $PDFCreator, $DefaultPrinter, $ReadyState, _
$i, $c, $AppTitle, $Scriptname, $ScriptBasename, $File


$fso = ObjCreate("Scripting.FileSystemObject")

$Scriptname = $fso.GetFileName(@ScriptFullPath)
$ScriptBasename = $fso.GetFileName(@ScriptFullPath)

$AppTitle = "PDFCreator - " & $ScriptBasename

$File = InputBox("FileName","Fill in the Path and filename","C:_AppsAutoIT3COMPDFCreatorVBScriptsGUI.vbs")

$PDFCreator = ObjCreate("PDFCreator.clsPDFCreator")
$PDFCreator.cStart ("/NoProcessingAtStartup")

With $PDFCreator
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveFormat") = 1 ; 0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII
$DefaultPrinter = .cDefaultprinter
.cDefaultprinter = "PDFCreator"
.cClearcache()
EndWith

; For $i = 0 to $objArgs.Count - 1
With $PDFCreator
$ifname = $File ;"C:TmpTest.xls" ;$objArgs($i)
If Not $fso.FileExists($ifname) Then
MsgBox (0,"Error","Can't find the file: " & $ifname & @CR & $AppTitle)
    Exit
EndIf
If Not .cIsPrintable(String($ifname)) Then
ConsoleWrite("Converting: " & $ifname & @CRLF & @CRLF & _
"An error is occured: File is not printable!" & @CRLF & $AppTitle & @CR)
EndIf

$ReadyState = 0
.cOption("AutosaveDirectory") = $fso.GetParentFolderName($ifname)
.cOption("AutosaveFilename") = $fso.GetBaseName($ifname)
.cPrintfile (String($ifname))
.cPrinterStop = 0

$c = 0
Do
$c = $c + 1
Sleep ($sleepTime)
Until ($ReadyState = 0) and ($c < ($maxTime * 1000 / $sleepTime))

If $ReadyState = 0 then
ConsoleWrite("Converting: " & $ifname & @CRLF & @CRLF & _
"An error is occured: File is not printable!" & @CRLF & $AppTitle & @CR)
    Exit
EndIf
EndWith
;Next

With $PDFCreator
.cDefaultprinter = $DefaultPrinter
.cClearcache()
    Sleep (200)
.cClose()
EndWith

ProcessClose("PDFCreator.exe")

;--- $PDFCreator events ---

Func PDFCreator_eReady()
$ReadyState = 1
EndFunc

Func PDFCreator_eError()
MsgBox(0, "An error is occured!" , "Error [" & $PDFCreator.cErrorDetail("Number") & "]: " & $PDFCreator.cErrorDetail("Description")& @CR)
EndFunc

There are lot's of VBScript Examples distributed with the installation. As well as for other Scripting or Programming languages.

Even for MS Office VBA :whistle:

So let's go and add your scripts to it.

Enjoy !!

ptrex

Edited by ptrex
Link to comment
Share on other sites

Handy to have.

Do you happen to know what the other possibilities are for .cOption("AutosaveFormat") ?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Link to comment
Share on other sites

@GEOSoft

I just discovered it half an hour ago.

So I can tell all options, but for sure TIFF and ASCII.

regards,

ptrex

OK, thanks for posting this info. Keep us updated. In the meantime I'm just downloading PDFCreator now. I've meant to do that for a long time anyway. I used to have a full load of Acrobat installed but since I had to replace a hard drive, I didn't re-install it this time. I'm Using FoxIt as a reader and I've tried using a Word add-in that I have for creating PDF's but that's kind of crappy and as slow as Acrobat.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@GEOSoft

These are all the file types I could find.

0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII

If you get some scripts translated post them. There are very interesting included !!

regards,

ptrex

Edited by ptrex
Link to comment
Share on other sites

Link to comment
Share on other sites

I'm trying to make time to play with it today but it's going to be touch & go. What you already have is great. I think I feel another UDF coming up. :whistle:

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Link to comment
Share on other sites

@GEOSoft

Great. Do some testing on the PDF2Email thing, it's not stable on my machine.

Added "CombineAndAddBoomarks" see first post.

regards,

ptrex

It's not too stable on mine either. I'll have another look at it later.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 1 month later...

Great stuff, so I wanted to create my little any2pdf-converter/binder.

Replaced the CreateTextfileAndPrint and it looks like this now:

CODE

Const $maxTime = 30    ; in seconds
Const $sleepTime = 250 ; in milliseconds

Dim $PDFCreator, $DefaultPrinter, $ReadyState, $fso, $c, $opath, _
$AppTitle, $ScriptBasename, $WshShell
$fso = ObjCreate("Scripting.FileSystemObject")
$ScriptBasename = $fso.GetBaseName(@ScriptFullPath)
$AppTitle = "PDFCreator - " & $ScriptBasename
$opath = $fso.GetParentFolderName(@ScriptFullPath)
$WshShell = ObjCreate("WScript.Shell")
;~ $WshShell.Popup ("Please wait a moment.", 2, $AppTitle, 64)

$PDFCreator = ObjCreate("PDFCreator.clsPDFCreator")
$PDFCreator.cStart ("/NoProcessingAtStartup")

With $PDFCreator
    .cPrinterStop = 1
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = $opath
    .cOption("AutosaveFilename") = $ScriptBasename
    $DefaultPrinter = .cDefaultprinter
    .cDefaultprinter = "PDFCreator"
    .cClearcache()
    CreateTextfileAndPrint('D:\Daten\Text\Samba\samba\www.oreilly.com\catalog\samba2\book\appa.html')
    CreateTextfileAndPrint('D:\Daten\Text\Samba\samba\www.oreilly.com\catalog\samba2\book\appb.html')
    Sleep (2000)                                        ; Wait until all files are printed
    .cCombineAll()
    $c = 0
    Do 
        $c = $c + 1
        Sleep ($sleepTime)
    until (.cCountOfPrintjobs <> 1) and ($c < ($maxTime * 1000 / $sleepTime))
    .cPrinterStop = 0
EndWith

$c = 0
$ReadyState = 0
Do 
    $c = $c + 1
    Sleep ($sleepTime)
Until ($ReadyState = 0) and ($c < ($maxTime * 1000 / $sleepTime))

If $ReadyState = 0 then
    ConsoleWrite("Creating test page as pdf.  "& "An error is occured: Time is up!  "  & @CR & $AppTitle  & @CR)
EndIf

With $PDFCreator
    .cDefaultprinter = $DefaultPrinter
    Sleep (200)
    .cClose()
EndWith

Func CreateTextfileAndPrint($Filename)
    If StringRegExp($Filename, '*.xls|*.doc|*.pdf') Then
        ShellExecuteWait($Filename, '', @ScriptDir, 'print')
    Else
        $IView=StringReplace(RegRead('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView', 'UninstallString'), 'v_uninstall', '_view32')
        RunWait('"'&$IView&'" "'&$Filename&'" /print', @ScriptDir)
    EndIf       
EndFunc

Func CompletePath($Path)
    Local $Return
    If StringRight($Path, 1) <> "\" Then
        $Return = $Path & "\"
    Else
        $Return = $Path
    EndIf
    Return $Return
EndFunc

;--- $PDFCreator events ---

Func PDFCreator_eReady()
    $ReadyState = 1
EndFunc

Func PDFCreator_eError()
    MsgBox(0, "An error is occured!" , "Error [" & $PDFCreator.cErrorDetail("Number") & "]: " & $PDFCreator.cErrorDetail("Description")& @CR)
EndFunc

All goes well untill I try do combine, that's when pdfcreator pops up and says it couldn't find gsdll32.dll, crashes or whatever. I can't do a regsvr32 on that file. ;( Can you confirm that or even tell me what you did to print other then plain-text files?

Can you tell me if there's a commandline-tool to do the printing? Maybe it would be possible to do it with a postscript-printer that prints into a file with the help of the plain gs.

PdfCreator itself is a little too "out of the box" to be controlled by controlclick/send and so on.

Edited by dabus
Link to comment
Share on other sites

  • 4 months later...

Hi Ptrex,

I am getting the following error when I tried creating an PDF using the first example from your first post:

Variable must be of type "Object".
$PDFCreator.cStart ("/NoProcessingAtStartup") 
$PDFCreator^ ERROR

I have Acrobat Reader and Writer versions 6.0 installed on my machine.

Please advise.

-DjDeep

Edited by DjDeep00
Link to comment
Share on other sites

Hi Ptrex,

I am getting the following error when I tried creating an PDF using the first example from your first post:

Variable must be of type "Object".
$PDFCreator.cStart ("/NoProcessingAtStartup") 
$PDFCreator^ ERROR

I have Acrobat Reader and Writer versions 6.0 installed on my machine.

Please advise.

-DjDeep

But do you have PDFCreator installed??

http://www.sf.net/projects/pdfcreator

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Ok thx Geosoft...I didnt know I had to install PDFCreator...I guess I missed that part...I was under the impression that you can create a PDF if you had PDF Writer installed. And for downloading which one do I need...

PDFCreator-0_9_3_GPLGhostscript.exe or PDFCreator-0_9_3_WithoutGhostscript.exe?

Thanks,

DjDeep

Link to comment
Share on other sites

Link to comment
Share on other sites

You can create PDF's with PDF writer through the COM inferface, but that's a different story.

Thanx Ptrex...I guess thats what I need. Kind of like how the Excel functions were made by Locodarwin. How would I do this? Edited by DjDeep00
Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...