Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell-Automated printing to pdf of a webpage

$
0
0

I am trying to automate the printing of a webpage using Powershell. Everything is running perfectly except for the naming of the pdf. I am trying to make it save the file as the current date, but for some reason it won't. If someone could help me, it will help my business a lot.

P.S. I would like the date to be in this format Month, Day, Year; and if the date or month are single digits, put a zero in front of them

Here is my code

##############################################################################

 

# provide the location where the PDF file will be created     #
$OUTPUT_FOLDER="C:\Users\Programming\Desktop\pdf"
 

#used to press enter to save file

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('splwow64')
Sleep 1

#making auto save date
$b = Get-Date
$ye = $b.year
$mo = $b.month
$da = $b.day
$a = "$mo $da $ye"
$c = Get-Date -format MM-dd-yyyy
 
write-host ($a)
write-host ($c)


   
# a name for the PDF file without the extension             
# without a name the file name is the current timestamp    
$OUTPUT_FILENAME= $c
 # comment following line to add a timestamp to each file gets created
if($OUTPUT_FILENAME.length -eq 0) {$OUTPUT_FILENAME=(Get-Date).tostring()}


#website url
$website=" "
################################################################
$ErrorActionPreference="Stop"
$WarningPreference="Stop"
$PDFINFOPATH="HKCU:\Software\Nitro\Reader\3.0\NitroPDFCreator\PDF"
$AUTOSAVEFNAMEPROPERTY="AutoSaveFilename"
$AUTOSAVEDIRPROPERTY="AutoSaveDirectory"
$USEAUTOSAVEPROPERTY="UseAutoSave"
################################################################

try
{
    get-itemproperty -path $PDFINFOPATH -name $AUTOSAVEDIRPROPERTY |out-null   
    set-itemproperty -path $PDFINFOPATH -name $AUTOSAVEDIRPROPERTY -value $OUTPUT_FOLDER |out-null    
}

catch
{
    new-itemproperty -path $PDFINFOPATH -name $AUTOSAVEDIRPROPERTY -value $OUTPUT_FOLDER |out-null   
    
}

 

 

 

try
{
    get-itemproperty -path $PDFINFOPATH -name $USEAUTOSAVEPROPERTY |out-null   
    set-itemproperty -path $PDFINFOPATH -name $USEAUTOSAVEPROPERTY -value "1" |out-null
}
catch
{
    new-itemproperty -path $PDFINFOPATH -name $USEAUTOSAVEPROPERTY -value "1" |out-null   
    
}


finally
{
  try
  {
    $ie=new-object -com internetexplorer.application
    $ie.visible = $true
    $ie.navigate($website)
    #depending upon the website sleep parameters here onwards may need adjustment
    start-sleep -seconds 5   

 

  
    try
    {
        get-itemproperty -path $PDFINFOPATH -name $AUTOSAVEFNAMEPROPERTY |out-null
        set-itemproperty -path $PDFINFOPATH -name $AUTOSAVEFNAMEPROPERTY -value "test.pdf" |out-null 


    }
    catch
    {
        new-itemproperty -path $PDFINFOPATH -name $AUTOSAVEFNAMEPROPERTY -value "test.pdf" |out-null

    } 


$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('splwow64')
Sleep 1
$wshell.SendKeys('~')


    start-sleep -seconds 5
    #next line opens print dialoge box 
    $ie.execWB(6,2)
 $wshell.SendKeys('~')
 $wshell.SendKeys('~')
 start-sleep -s 1
    start-sleep -seconds 5    
    $ie.quit() 

  }
  catch {}
  finally
  {  
    try
    {

        set-itemproperty -path $PDFINFOPATH -name $AUTOSAVEFNAMEPROPERTY -value "" |out-null 
        set-itemproperty -path $PDFINFOPATH -name $AUTOSAVEDIRPROPERTY -value "" |out-null 
        set-itemproperty -path $PDFINFOPATH -name $USEAUTOSAVEPROPERTY -value "0" |out-null 
    }
    catch{}    
  }  
}
start-sleep -seconds 5


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>