Skip to main content
August 18, 2017
Answered

html to PDF using VBS

  • August 18, 2017
  • 1 reply
  • 6360 views

I need a .VBS example of how to read in an html file into Acrobat DC and then save the file as a PDF.

    This topic has been closed for replies.
    Correct answer

    Here is the code me and another guy came up with.  It works entirely in the background so you won't see an Acrobat icon while it is converting the html file into a pdf. I hope you like it.

    Option Explicit

    Dim AcroApp
    Dim objAVDoc
    Dim objPDDoc

    Set AcroApp = CreateObject("AcroExch.App")
    AcroApp.Hide

    Set objAVDoc = CreateObject("AcroExch.AVDoc")
    'Input file path
    objAVDoc.Open "C:\Temp\Do_Loop_Example.html", ""

    Set objPDDoc = objAVDoc.GetPDDoc
    'Output file path
    objPDDoc.Save 1, "C:\Temp\Do_Loop_Example.pdf" 'Explicit save.

    objPDDoc.Close
    objAVDoc.Close -1 'Close without saving -- an explicit save was already invoked.

    Set objPDDoc = Nothing
    Set objAVDoc = Nothing

    AcroApp.Exit
    Set AcroApp = Nothing

    1 reply

    August 24, 2017

    Here is the code I came up with but it does not automatically save the pdf file. And the file Junk.Pdf is really junk but without it sometimes I get an error the file is already open. When it closes it does prompt me to save the file to a path of my choosing. It works but it is not anywhere close to what I want. What I want is for it to save the pdf file with the same name as the html file without me having to do anything other than run the code. Saving the PDF file in the same path as the html file would be great.

    Dim AcroApp

    Set AcroApp = CreateObject("AcroExch.App")

    Set objPDDocNew = CreateObject("AcroExch.PDDoc")

    objPDDocNew.Create()

    Set objPDDoc = CreateObject("AcroExch.AVDoc")

    objPDDoc.Open "C:\Temp\Do_Loop_Example.html", ""

    objPDDocNew.Save 1, "C:\Temp\Junk.pdf"

    objPDDoc.Close 0

    AcroApp.Exit

    Correct answer
    August 25, 2017

    Here is the code me and another guy came up with.  It works entirely in the background so you won't see an Acrobat icon while it is converting the html file into a pdf. I hope you like it.

    Option Explicit

    Dim AcroApp
    Dim objAVDoc
    Dim objPDDoc

    Set AcroApp = CreateObject("AcroExch.App")
    AcroApp.Hide

    Set objAVDoc = CreateObject("AcroExch.AVDoc")
    'Input file path
    objAVDoc.Open "C:\Temp\Do_Loop_Example.html", ""

    Set objPDDoc = objAVDoc.GetPDDoc
    'Output file path
    objPDDoc.Save 1, "C:\Temp\Do_Loop_Example.pdf" 'Explicit save.

    objPDDoc.Close
    objAVDoc.Close -1 'Close without saving -- an explicit save was already invoked.

    Set objPDDoc = Nothing
    Set objAVDoc = Nothing

    AcroApp.Exit
    Set AcroApp = Nothing