Sign in to follow this  
Followers 0

PDFCreator - Print2PDF

38 posts in this topic

Posted (edited)

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
mLipok likes this

Share this post


Link to post
Share on other sites



Posted

Handy to have.

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

Share this post


Link to post
Share on other sites

Posted

@GEOSoft

other possibilities are for .cOption("AutosaveFormat")

I just discovered it half an hour ago.

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

regards,

ptrex

Share this post


Link to post
Share on other sites

Posted

@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.

Share this post


Link to post
Share on other sites

Posted

I didn't know either, thanks ptrex. :whistle:

Share this post


Link to post
Share on other sites

Posted (edited)

@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

Share this post


Link to post
Share on other sites

Posted

@all

I added "Testpage2PDFSendEmail" as well.

But is seems not to do it at all times. Needs more experimenting.

Let me know how it runs on other machines.

thanks

ptrex

Share this post


Link to post
Share on other sites

Posted (edited)

@All

Add "Convert2PDF" and "CombineJobs" and "PrintJobInfo"

So that's all for know.

Regards,

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

Posted

Looks very promissing. Thanks for sharing.

Share this post


Link to post
Share on other sites

Posted

@Josbe / Zedna,

You are welcome !! :whistle:

Regards,

ptrex

Share this post


Link to post
Share on other sites

Posted

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:

Share this post


Link to post
Share on other sites

Posted (edited)

@GEOSoft

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

Added "CombineAndAddBoomarks" see first post.

regards,

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

Posted

@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.

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

@DjDeep00

I recommend to take the first one : PDFCreator-0_9_3_GPLGhostscript.exe

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

regards,

ptrex

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

@DjDeep00

You don't need to do anything, because someone else did it for you.

Acrobat UDF

Regards,

ptrex

Edited by ptrex

Share this post


Link to post
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
Sign in to follow this  
Followers 0