Results 1 to 37 of 37

Thread: Save PDF file with VBs Printer Object

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Save PDF file with VBs Printer Object

    Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.

    HTH
    Attached Files Attached Files

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.

    HTH
    Yes, API hooking would the thermonuclear option here :-))

    cheers,
    </wqw>

  3. #3
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.
    HTH
    Eduardo - this code doesn't seem to work for me on Win10? i just get printer error 482? i have this Win10 machine configured with only printer installed is "Microsoft Print to PDF". Should your code be running as-is?
    /Jimboat

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by Jimboat View Post
    Eduardo - this code doesn't seem to work for me on Win10? i just get printer error 482? i have this Win10 machine configured with only printer installed is "Microsoft Print to PDF". Should your code be running as-is?
    Change the path "D:\test.pdf" to whatever is valid on your system.

  5. #5
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Change the path "D:\test.pdf" to whatever is valid on your system.
    Yes, i changed to "C:\test.pdf". does 'test.pdf' have to exist already? maybe C:\ doesn't like write to?
    /Jimboat

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by Jimboat View Post
    does 'test.pdf' have to exist already?
    No.

    Quote Originally Posted by Jimboat View Post
    maybe C:\ doesn't like write to?
    Yes, you are not allowed to write to the root C:\
    Make a folder and write to it, like C:\New folder\test.pdf

  7. #7
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.

    HTH
    What is the purpose or principle of mReplaceAPI?

    Print to specific PDF file name
    Assumes Windows 10 with the optional Microsoft Print to PDF driver installed. This is normally the default unless the user has removed it via Windows Features.

    Bare bones example. Note that for the most part printing must be done via API calls to avoid raising the driver's file picker dialog
    easy understand...

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by DaveDavis View Post
    What is the purpose or principle of mReplaceAPI?
    To hook the call to the StartDoc API to be able to set the file.

  9. #9
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Yes, you are not allowed to write to the root C:\
    Make a folder and write to it, like C:\New folder\test.pdf
    Yes, that was all that the problem was. thanks, Edwardo. your program works great.

    Add'l question....
    is there a way to autofill the Windows dialog the "Microsoft Print to PDF" presents to the user, and allow the dialog to remain operational? One of the issues that users have is that the windows dialog initially shows with no filename, and users get error if they click either 'Save' or 'Cancel' without properly entering a valid filename. Yes, the users should pay closer attention to their entry and random clicking, but a preventative solution would be to autofill a default FileName, while still allowing the user to use the windows dialog to change directory selection or change the filename if desired.
    /Jimboat

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by Jimboat View Post
    Yes, that was all that the problem was. thanks, Edwardo. your program works great.

    Add'l question....
    is there a way to autofill the Windows dialog the "Microsoft Print to PDF" presents to the user, and allow the dialog to remain operational? One of the issues that users have is that the windows dialog initially shows with no filename, and users get error if they click either 'Save' or 'Cancel' without properly entering a valid filename. Yes, the users should pay closer attention to their entry and random clicking, but a preventative solution would be to autofill a default FileName, while still allowing the user to use the windows dialog to change directory selection or change the filename if desired.
    I think you can't, because if you set the name, then it doesn't ask.
    You will have to add your own common dialog Save As to do that.

  11. #11
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    I think you can't, because if you set the name, then it doesn't ask.
    You will have to add your own common dialog Save As to do that.
    ok. thanks again, Eduardo.
    /jim
    /Jimboat

  12. #12
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    558

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.

    HTH
    Works perfectly. Extremely simple to use. I like it! Does it store the default printer and set it back? (I have mine set to Microsoft PDF already so I didn't check). Thanks!

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by Darkbob View Post
    Does it store the default printer and set it back?
    No, but easy to add that:

    Code:
    Private Sub Command1_Click()
        Dim strCurrentPrn As String
        
        strCurrentPrn = Printer.DeviceName
        If Not SelectPrinter("Microsoft Print to PDF") Then
            MsgBox "Can't save PDF, 'Microsoft Print to PDF' driver not present", vbCritical
            Exit Sub
        End If
        
        PrinterFilePath = "D:\test.pdf"
        
        Printer.Print "AAA"
        Printer.FontSize = 34
        Printer.Print "BBB"
        Printer.DrawWidth = 10
        Printer.Circle (1000, 1000), 500, vbRed
        Printer.EndDoc
        
        PrinterFilePath = ""
        SelectPrinter strCurrentPrn
    End Sub

  14. #14
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: Print to specific PDF file name

    Quote Originally Posted by Eduardo- View Post
    To hook the call to the StartDoc API to be able to set the file.
    Can this module be used as common function to hook other APIs? e.g. hook TextBox DrawTextA/W API so that we can print colorful text?

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Print to specific PDF file name

    Quote Originally Posted by DaveDavis View Post
    Can this module be used as common function to hook other APIs? e.g. hook TextBox DrawTextA/W API so that we can print colorful text?
    Yes, I've hooked other APIs.

  16. #16
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: Save PDF file with VBs Printer Object

    Could I ask what the difference is between your work and this one vbImg2Pdf

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by loquat View Post
    Could I ask what the difference is between your work and this one vbImg2Pdf
    I don't think the two projects have much in common.

    vbimg2pdf:
    Convert jpeg/png images to multi-page pdf file
    You can do that also with this project, but with this project you can print anything, not just images.
    And the most important feature is that you do it with the Printer object.
    On the other and, it seems that that project splits automatically the images into several PDF pages. I don't think the Printer object will do that automatically.

    So, basically, they are two completely different projects, with different goals.

    Also, another key difference is that this method requires the "Microsoft Print to PDF" driver installed.

  18. #18
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: Save PDF file with VBs Printer Object

    yes, have tested your code, very impressive
    Code:
        Printer.NewPage
        Printer.PaintPicture LoadPicture("c:\1.jpg"), 0, 0

  19. #19
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Hi, someone can guide me to use this apis in order to save a datareport to PDF, using a pdf printer (Microsoft print ot pdf)?

    i tried everything without success :-(

    Thanks in advance
    Last edited by Calcu; May 4th, 2022 at 05:07 AM.

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by Calcu View Post
    Hi, someone can guide me to use this apis in order to save a datareport to PDF, using a pdf printer (Microsoft print ot pdf)?

    i tried everything without success :-(

    Thanks in advance
    If you could post some example project where you say you experience the problem maybe we could investigate what is happening and look for a solution. (PS: a simple project)

  21. #21
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Hi, thanks for answering.

    I found "why it wasn't working", i used your code from the first post, but it never filled the pdf file, and today... looking quietly my code, i found that i was setting the "microsoft printer to pdf " with a loop and "set printer = x", BUT not with the API that sets the default printer.

    Once i selected the Microsoft Printer to PDF as "default", not only with the set printer... it worked like a charm.

    Thanks a lot for your code !

  22. #22
    Addicted Member
    Join Date
    Aug 2013
    Posts
    236

    Re: Save PDF file with VBs Printer Object

    Me too! Just used this today and works great. Thanks Eduardo!

  23. #23
    Member
    Join Date
    Mar 2020
    Posts
    33

    Re: Save PDF file with VBs Printer Object

    Your code is so clever! It is really useful to me.

    Thank you

  24. #24
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    It was working fine until today.

    I found I need to start the exe with admin privileges in in order to work again (windows 10),

    Any chance to make it work again without using as admin the app?

    Thanks !

  25. #25

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Just tested:
    I downloaded the sample project from the first post.
    Compiled the exe.
    Ran it and clicked the button.
    The file d:\test.pdf was created as expected.

    Windows 11 22H2 with up to date automatic updates installed.

  26. #26
    Junior Member
    Join Date
    Jun 2017
    Posts
    26

    Re: Save PDF file with VBs Printer Object

    My project runs fine in the IDE and produces a perfect PDF report.

    However when I try to compile I get the following error.

    ---------------------------
    Microsoft Visual Basic
    ---------------------------
    Programmatic ID string too long 'TracsDashboard.cAutoRestoreFunctionOnExit'. The Programmatic ID must be 39 characters or less.
    ---------------------------
    OK Help
    ---------------------------

  27. #27

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by cidtek View Post
    My project runs fine in the IDE and produces a perfect PDF report.

    However when I try to compile I get the following error.

    ---------------------------
    Microsoft Visual Basic
    ---------------------------
    Programmatic ID string too long 'TracsDashboard.cAutoRestoreFunctionOnExit'. The Programmatic ID must be 39 characters or less.
    ---------------------------
    OK Help
    ---------------------------
    The project name and a Class name cannot exceed 39 characters as the error message implies.
    So for your project named TracsDashboard then change cAutoRestoreFunctionOnExit class name to something shorter.

  28. #28
    Junior Member
    Join Date
    Jun 2017
    Posts
    26

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by Eduardo- View Post
    The project name and a Class name cannot exceed 39 characters as the error message implies.
    So for your project named TracsDashboard then change cAutoRestoreFunctionOnExit class name to something shorter.
    Thanks that did it!

    I understood what the message implied but I wasn't sure if a declaration could be changed instead of changing names.

  29. #29
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Oh..
    You are right, I tried the sample and worked fine...

    I have to find then why when I use the printer.enddoc statement, my programs ends.

    An API conflict ? I must recheck then everything :-(

    Thanks !

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by Calcu View Post
    An API conflict ?
    Ensure that you have the API declarations as provided in the sample.

  31. #31
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Checked, they are fine, i will start a new project adding slowly all the .bas files.

    I think the new vbflexgrid from Krool it's making some kind of problem, i deleted (in a copy) the uctabmdi files, the sendmail and some other with apis ...

    i checked that no public apis exists inside the project...

    Weird.. i have to spend more time finding where this problem is :_(

  32. #32

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Just copy the files mReplaceAPI.bas and AutoRestoreFunctionOnExit.cls as they are here and it should work fine, since the APIs are all there and are declared Private.

    If it still crashes then it must be something else.

  33. #33
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Hi, thanks again, i found what it was!

    I used the code to save a datareport to a pdf from a Mdi form. Everything was ok.

    But i had to change the form from Mdi child to one normal, but... modal...

    And.. i was still launching the datareport whitout being modal .... , (i can launch a normal form / datareport from a modal form, without doing it modal too) the strange thing is no error was sent anywhere... but i found it finally.. working like a charm again!

    Thanks!

  34. #34
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by Eduardo- View Post
    Just copy the files mReplaceAPI.bas and AutoRestoreFunctionOnExit.cls as they are here and it should work fine, since the APIs are all there and are declared Private.

    If it still crashes then it must be something else.
    Why do you create some amazing projects every time? I suspect that you have worked in Microsoft for several months.

  35. #35

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    Quote Originally Posted by xiaoyao View Post
    Why do you create some amazing projects every time? I suspect that you have worked in Microsoft for several months.
    Thanks for your compliment but no, I didn't work in MS and I'm from Argentina.

  36. #36
    Lively Member
    Join Date
    May 2022
    Posts
    84

    Re: Save PDF file with VBs Printer Object

    Hi again :-(

    I need a little help, i don't know what else i have to do.

    Your example works always, it's perfect.

    I want to print a datareport to pdf, and i use your code.

    My program:

    In IDE it works 99% of time.
    In compiled exe, it only works 1% time.

    Normally it hangs and closes everything.

    I'm doing:

    1.- creating a recordset
    2.- using a datareport without the .show instruction (if i use the .show, it works fine and i can see the datareport fine)


    Then, instead of showing the datareport, i use:

    Code:
    PrinterFilePath = "c:\temp\example.pdf" 
    
    Informe_ECI.PrintReport
    
    Printer.EndDoc
    
    PrinterFilePath = ""

    When it arrives to printerfilepath="" and start it, is when it usually hangs.... (my program closes, without error)

    Tested in windows 10 (my development machine is win 10 too), and windows 11...

    I'm pretty sure it's a problem from the datareport part, but...

    Any idea ?

    Thanks in advance !


    Edit: !!

    Solved compiling the program as P-Code

    Thanks!
    Last edited by Calcu; Mar 28th, 2023 at 10:25 AM.

  37. #37

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Save PDF file with VBs Printer Object

    As the title says, this code is for saving PDF files with "VB's Printer object".

    DataReport <> Printer object.

    Anyway the code should work also transparently for any other component that used the StartDocA API.

    But, you shouldn't mix DataReport commands with Printer.EndDoc, they are different objects/components.

    For working with other components, you need to ensure that before starting a print job, the component will use the "Microsoft Print to PDF" driver for printing.

    That's done for the Printer object in the sample code in the SelectPrinter procedure, but that will only select the current printer for the Printer object, not for other components.

    I don't remember how to select the current printer in DataReport (look on internet, or in this forum, or ask in the general forum, or maybe someone seeing post it here).

    And at the end, let the component (DataReport in this case) do the "EndDoc" for itself, don't call Printer.EndDoc because it has nothing to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width