Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Create/Convert to PDF w/ PDFCreator [COM] [AHK_L]


  • Please log in to reply
10 replies to this topic
shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Create or convert documents to PDF with PDFCreator COM interface. First you'll have to download and install PDFCreator

IMPORTANT: How to Install PDFCreator without adware

Create PDFs from any program that is able to print
Security: Encrypt PDFs and protect them from being opened, printed etc
New: Digitally sign your PDFs to ensure that you are the author and the file has not been modified
New: Create PDF/A files for long term archives
Send generated files via eMail
Create more than just PDFs: PNG, JPG, TIFF, BMP, PCX, PS, EPS
AutoSave files to folders and filenames based on Tags like Username, Computername, Date, Time etc.
Merge multiple files into one PDF
Easy Install: Just say what you want and everything is installed
Terminal Server: PDFCreator also runs on Terminal Servers without problems
And the best: PDFCreator is free, even for commercial use! It is Open Source and released under the Terms of the GNU General Public License.
Supports Windows Vista and Windows 7


They have provided many example files:
CombineAndAddBookmarks.vbs               SaveOptionsToFile.vbs
CombineJobs.vbs                          ShowLogfile.vbs
CompareColorCompressionModes.vbs         ShowOptions.vbs
Convert2PDF.vbs                          ShowPrintjobInfos.vbs
Convert2PDFAndPrint.vbs                  TestCompression1.vbs
Convert2TIFF.vbs                         TestCompression2.vbs
Convert2TXT.vbs                          TestCompression3.vbs
ConvertJPEG2PDF.vbs                      TestEvents.vbs
GhostscriptDirect.vbs                    Testpage2PDF.vbs
GUI.vbs                                  Testpage2PDFSendEmail.vbs
PS2PDF.vbs                               URL2PDF.vbs

I am converting "Convert2PDF.vbs" and "Testpage2PDF.vbs" to AHK as examples here to get you started.
PS: After installing PDFCreator, i had to restart my computer once to get it working.
; Testpage2PDF script
; Homepage: http://www.pdfforge.org/ - Version: 1.2.0
; Comments: Save the test page as pdf-file with PDFCreator.

maxTime = 10    ; in seconds
sleepTime = 250 ; in milliseconds
PDFCreator$Ready := 0 , i := 0

PDFCreator := ComObjCreate("PDFCreator.clsPDFCreator")
ComObjConnect(PDFCreator, "PDFCreator_")
PDFCreator.cStart("/NoProcessingAtStartup")


PDFCreator.cOption("UseAutosave") := 1
PDFCreator.cOption("UseAutosaveDirectory") := 1
PDFCreator.cOption("AutosaveDirectory") := A_ScriptDir
PDFCreator.cOption("AutosaveFilename") := "Testpage - PDFCreator"
PDFCreator.cOption("AutosaveFormat") := 0   ; 0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII
DefaultPrinter := PDFCreator.cDefaultprinter
PDFCreator.cDefaultprinter := "PDFCreator"
PDFCreator.cClearcache()
PDFCreator.cPrintPDFCreatorTestpage()
PDFCreator.cPrinterStop := 0

While (PDFCreator$Ready = 0) and (i < (maxTime * 1000 / sleepTime)) 
{
i++
Sleep %sleepTime%
}

PDFCreator.cDefaultprinter := DefaultPrinter
Sleep 200
PDFCreator.cClose()

If (PDFCreator$Ready = 0)
 MsgBox, Error - TimeOut!

;--- PDFCreator events ---

PDFCreator_eReady() {
 global PDFCreator$Ready
 PDFCreator$Ready = 1
}

PDFCreator_eError() {
 MsgBox % "An error is occured!`n`nError [" . PDFCreator.cErrorDetail("Number") . "]: " . PDFCreator.cErrorDetail("Description")
}

; Convert2PDF script
; Homepage: http://sourceforge.net/projects/pdfcreator - Version: 1.2.0
; Comments: Convert selected file to PDF

maxTime = 30    ; in seconds
sleepTime = 250 ; in milliseconds
PDFCreator$Ready := 0 , i := 0
FileSelectFile, ifname, 3, ,Select file to print..
SplitPath, ifname, ,,,ifnamenoext

PDFCreator := ComObjCreate("PDFCreator.clsPDFCreator")
ComObjConnect(PDFCreator, "PDFCreator_")
PDFCreator.cStart("/NoProcessingAtStartup")


PDFCreator.cOption("UseAutosave") := 1
PDFCreator.cOption("UseAutosaveDirectory") := 1
PDFCreator.cOption("AutosaveDirectory") := A_ScriptDir
PDFCreator.cOption("AutosaveFilename") := ifnamenoext . ".pdf"
PDFCreator.cOption("AutosaveFormat") := 0                        ; 0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII
DefaultPrinter := PDFCreator.cDefaultprinter
PDFCreator.cDefaultprinter := "PDFCreator"
PDFCreator.cClearcache()

If Not PDFCreator.cIsPrintable(ifname)
 {
 MsgBox % "Error: File is not printable!"
 ExitApp
 }

  PDFCreator.cPrintfile(ifname)
  PDFCreator.cPrinterStop := 0

While (PDFCreator$Ready = 0) and (i < (maxTime * 1000 / sleepTime)) 
{
i++
Sleep %sleepTime%
}

PDFCreator.cDefaultprinter := DefaultPrinter
Sleep 200
PDFCreator.cClose()

If PDFCreator$Ready = 0
  MsgBox % "Error printing!"

;--- PDFCreator events ---

PDFCreator_eReady() {
 global PDFCreator$Ready
 PDFCreator$Ready = 1
}

PDFCreator_eError() {
 MsgBox % "An error is occured!`n`nError [" . PDFCreator.cErrorDetail("Number") . "]: " . PDFCreator.cErrorDetail("Description")
}

If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

Jobine
  • Members
  • 3 posts
  • Last active: Mar 02 2011 03:38 PM
  • Joined: 02 Mar 2011
I'm not an expert in VBS programming, I watched your scripts, but I'm unable to do the script I want.

This is the print pdf document and send it by email to a pre-determined email address... as in your Testpage2PDFSendEmail.vbs, but I don't want to print the test page, but my document. No matter what software I use, I print and get the pdf via email.

Can you help me with this script special ?

On the web, I found some scripts for Excel, but it's not my case.
See the code below, I think the line has changed is:
ActiveSheet.PrintOut Copies: = 1, ActivePrinter: = "PDFCreator"

Thank's

Sub Tst_EmailPdfCreator()
Dim objMessage
Dim JobPDF 
Dim sNomPDF 
Dim sCheminPDF
    
    sNomPDF = "Essai_PdfCreator.pdf"
    sCheminPDF = ThisWorkbook.Path & "\"

    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

    Set JobPDF = CreateObject("PDFCreator.clsPDFCreator")

    With JobPDF
        If .cStart("/NoProcessingAtStartup") = False Then
            MsgBox "Initialisation de PDFCreator impossible", vbCritical + vbOKOnly, "PDFCreator"
            Exit Sub
        End If
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sCheminPDF
        .cOption("AutosaveFilename") = sNomPDF

        '   0=PDF, 1=Png, 2=jpg, 3=bmp, 4=pcx, 5=tif, 6=ps, 7=eps, 8=txt
        .cOption("AutosaveFormat") = 0

        .cClearCache
    End With

    ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"

    'Fichier dans la file d'attente
    Do Until JobPDF.cCountOfPrintjobs = 1
        DoEvents
    Loop
    JobPDF.cPrinterStop = False

    'Attendre que la file d'attente soit vide
    Do Until JobPDF.cCountOfPrintjobs = 0
        DoEvents
    Loop

    JobPDF.cClose
    Set JobPDF = Nothing

    Set objMessage = CreateObject("CDO.Message")
    With objMessage
        .Subject = "Example"
        .From = "[email protected]"
        .To = "[email protected]"
        .TextBody = "Texte dans le corps de message"
        .AddAttachment sCheminPDF & sNomPDF
        .Send
    End With

    Set objMessage = Nothing
End Sub


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Just use my second example.

With the file created, send it via email using this email delivery script.
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

Jobine
  • Members
  • 3 posts
  • Last active: Mar 02 2011 03:38 PM
  • Joined: 02 Mar 2011
that script suppose to work if I use it in "RunProgramAfterSaving" ?

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

that script suppose to work if I use it in "RunProgramAfterSaving" ?

what do you mean? I did not understand your question.
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

Jobine
  • Members
  • 3 posts
  • Last active: Mar 02 2011 03:38 PM
  • Joined: 02 Mar 2011
In PDFCreator after the PDF generation, there is a special place for it.

How I can execute your script otherwise ? or how I can use it ?

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Just change the marked fields, and you are good to go..

; Convert2PDF script
; Homepage: http://sourceforge.net/projects/pdfcreator - Version: 1.2.0
; Comments: Convert selected file to PDF

maxTime = 30    ; in seconds
sleepTime = 250 ; in milliseconds
PDFCreator$Ready := 0 , i := 0
; FileSelectFile, ifname, 3, ,Select file to print..
ifname := "c:\path\to\your\file.ext"  ; <-- INSERT YOUR FILE HERE
SplitPath, ifname, ,,,ifnamenoext

PDFCreator := ComObjCreate("PDFCreator.clsPDFCreator")
ComObjConnect(PDFCreator, "PDFCreator_")
PDFCreator.cStart("/NoProcessingAtStartup")


PDFCreator.cOption("UseAutosave") := 1
PDFCreator.cOption("UseAutosaveDirectory") := 1
PDFCreator.cOption("AutosaveDirectory") := A_ScriptDir
IfExist, %ifnamenoext%.pdf
  FileDelete, %ifnamenoext%.pdf
PDFCreator.cOption("AutosaveFilename") := ifnamenoext . ".pdf"
PDFCreator.cOption("AutosaveFormat") := 0                        ; 0=PDF, 1=PNG, 2=JPG, 3=BMP, 4=PCX, 5=TIFF, 6=PS, 7= EPS, 8=ASCII
DefaultPrinter := PDFCreator.cDefaultprinter
PDFCreator.cDefaultprinter := "PDFCreator"
PDFCreator.cClearcache()

If Not PDFCreator.cIsPrintable(ifname)
 {
 MsgBox % "Error: File is not printable!"
 ExitApp
 }

  PDFCreator.cPrintfile(ifname)
  PDFCreator.cPrinterStop := 0

While (PDFCreator$Ready = 0) and (i < (maxTime * 1000 / sleepTime)) 
{
i++
Sleep %sleepTime%
}

PDFCreator.cDefaultprinter := DefaultPrinter
Sleep 200
PDFCreator.cClose()

If PDFCreator$Ready = 0
  MsgBox % "Error printing!"
  
pmsg          := ComObjCreate("CDO.Message")
pmsg.From       := """AHKUser"" <[email protected]>" ; <-- INSERT FROM ADDRESS HERE
pmsg.To       := "[email protected]"         ; <-- INSERT TO ADDRESS HERE
;pmsg.BCC       := ""   ; Blind Carbon Copy, Invisable for all, same syntax as CC
;pmsg.CC       := "[email protected], [email protected]"
pmsg.Subject    := "Message_Subject"             ; <-- INSERT EMAIL SUBJECT HERE

;You can use either Text or HTML body like
pmsg.TextBody    := "Message_Body"
;OR
;pmsg.HtmlBody := "<html><head><title>Hello</title></head><body><h2>Hello</h2><p>Testing!</p></body></html>"


sAttach         := ifnamenoext . ".pdf" ; can add multiple attachments, the delimiter is |

fields := Object()
fields.smtpserver   := "smtp.gmail.com"          ; <-- specify your SMTP server
fields.smtpserverport     := 465 ; 25
fields.smtpusessl      := True ; False
fields.sendusing     := 2   ; cdoSendUsingPort
fields.smtpauthenticate     := 1   ; cdoBasic
fields.sendusername := "[email protected]"           ; <-- INSERT USERNAME HERE
fields.sendpassword := "your_password_here"      ; <-- INSERT PASSWORD HERE
fields.smtpconnectiontimeout := 60
schema := "http://schemas.microsoft.com/cdo/configuration/"


pfld :=   pmsg.Configuration.Fields

For field,value in fields
   pfld.Item(schema . field) := value
pfld.Update()

Loop, Parse, sAttach, |, %A_Space%%A_Tab%
  pmsg.AddAttachment(A_LoopField)
pmsg.Send()


;--- PDFCreator events ---

PDFCreator_eReady() {
 global PDFCreator$Ready
 PDFCreator$Ready = 1
}

PDFCreator_eError() {
 MsgBox % "An error is occured!`n`nError [" . PDFCreator.cErrorDetail("Number") . "]: " . PDFCreator.cErrorDetail("Description")
}

If you still don't get it, consider spending some time trying to learn the basics of Autohotkey before you try to script this.
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Edit: remark about adware removed, see instructions below for further info

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
@hugov : i did look at libharu before wrapping PDFcreator.
libharu is not good for converting existing files, html, word or excel to PDF. So this is the only easy solution for this specific end.
Also, it did not install any crappy redirect (atleast not in firefox and chrome, i rarely if ever use IE).
Does the red highlight require to be so on your face :roll: ?
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Edit: see post below for info

There is is a lot of controversy with PDFCreator and people downloading it and simply doing a click click click install may end up with software and AV warnings they don't want, read more about it here:
<!-- m -->http://en.wikipedia.... ... of_malware<!-- m -->
I think mentioning it is essential.


shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
How to Install PDFCreator without adware:

Install as you normally would. But when you reach the screen (shown below), uncheck "I agree to the Terms of Use and Privacy Policy and wish to install pdfforge Toolbar."
Posted Image
Now continue the installation as you would normally do. This tut was made for PDFCreator 1.2.0. If you are installing a newer version, be careful and read all options before installing.
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor