使用PDFCreate 和 Powershell 自动保存网页为PDF

  • 先安装PDF Creator。

  http://rj.baidu.com/soft/detail/10500.html?ald

  • 把他设置为默认打印机。
  • 在IE中设置打印页面的边距,页眉页脚等。

Powershell脚本

[CmdletBinding()]
param (

    [Parameter(Mandatory=$False, Position=0, ValueFromPipeline=$false, HelpMessage='Define the Key')]
    [ValidateNotNullOrEmpty()]
    [string]$WorkPlace,
    [Parameter(Mandatory=$False, Position=0, ValueFromPipeline=$false, HelpMessage='Mail to users split with , ')]
    [ValidateNotNullOrEmpty()]
    [string[]]$MailTos
    
)
 
# provide the location where the PDF file will be created     # 
$OUTPUT_FOLDER="D:\TimeJob\" 
 
# a name for the PDF file without the extension               # 
# without a name the file name is the current timestampt      #  
$OUTPUT_FILENAME="" 
 
# comment following line to add a timestamp to each file gets created 
if($OUTPUT_FILENAME.length -eq 0) {$OUTPUT_FILENAME="$WorkPlace" +"_" + (get-date).tostring("yyyyMMdd_HHmmss")} 
 
#website 
$website="http://www.baidu.com" 

#$website="https://www.baidu.com/"
############################################################### 
#         DO NOT WRITE ANYTHING BELOW THIS LINE               # 
############################################################### 
$ErrorActionPreference="Stop" 
$WarningPreference="Stop" 
$PDFINFOPATH="HKCU:\Software\PDFCreator\Program" 
$AUTOSAVEFNAMEPROPERTY="AutoSaveFilename" 
$AUTOSAVEDIRPROPERTY="AutoSaveDirectory" 
$USEAUTOSAVEPROPERTY="UseAutoSave" 
################################################################ 


function WriteScreenLog {
##
## Write a log entry to the screen
## Usage:
##        WriteScreenLog -Message "Message text" [-Type OK|Warning|Error|Info|Verbose] [-printTime]

    Param (
        [Parameter(Mandatory=$True,Position=0)]
        [string]$Message,
              
        [ValidateSet("OK","Warning","Error", "Info", "Verbose")] 
        [string]$Type,
              
        [switch]$printTime
    )
    $screenXpos = [Math]::Truncate($Host.UI.RawUI.WindowSize.Width - 11)

    # Write the message to the screen
    $now = ""
    if($printTime -eq $true){
            $now = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
            $now = "$now | "
    }
    $Message = $now + $Message
    Write-Output $Message
       
    if($Type -ne "") {
    [Console]::SetCursorPosition($screenXpos, $Host.UI.RawUI.CursorPosition.Y-1)
    }
    switch ($Type) {
            "OK" {Write-Host -BackgroundColor Green -ForegroundColor Black  "    OK    "}
            "Warning" {Write-Host -BackgroundColor Yellow -ForegroundColor Black "  Warning "}
            "Error" {Write-Host -ForegroundColor Yellow -BackgroundColor Red  "   Error  "}
            "Info" {Write-Host -BackgroundColor $Host.UI.RawUI.ForegroundColor -ForegroundColor $Host.UI.RawUI.BackgroundColor "   Info   "}
            "Verbose" {Write-Host -BackgroundColor $Host.UI.RawUI.ForegroundColor -ForegroundColor $Host.UI.RawUI.BackgroundColor "  Verbose "}
    }
} 

################################################################
$LogFile="D:\TimeJob\Log\$OUTPUT_FILENAME.txt"
Start-Transcript -Path $LogFile

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.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 "$OUTPUT_FILENAME.pdf" |out-null 
         
    } 
    catch 
    { 
        new-itemproperty -path $PDFINFOPATH -name $AUTOSAVEFNAMEPROPERTY -value "$OUTPUT_FILENAME.pdf" |out-null     
    }        
    start-sleep -seconds 5     
    $ie.execWB(6,2) 
    start-sleep -seconds 5     
    $ie.quit()    
    WriteScreenLog -Message "Success:Save $OUTPUT_FOLDER$OUTPUT_FILENAME.pdf"  
    #WriteScreenLog -Message "" -Type INFO -printTime     
  } 
  catch 
  {
    WriteScreenLog -Message "Error:Save $OUTPUT_FOLDER$OUTPUT_FILENAME.pdf" -Type ERROR -printTime  
  } 
  finally 
  { 
    try
    {   
        WriteScreenLog -Message "Start to sleep 10 seconds for save PDF report"
        Start-Sleep -s 10
        WriteScreenLog -Message "Success:Send $OUTPUT_FOLDER$OUTPUT_FILENAME.pdf to $MailTos"
        #Start-Sleep -s 60 
    }
    catch
    {
        WriteScreenLog -Message "Error:Send $OUTPUT_FOLDER$OUTPUT_FILENAME.pdf to $MailTos"
        sendReportEmailError "Error:Send $OUTPUT_FOLDER$OUTPUT_FILENAME.pdf to $MailTos"
    }
    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
    {
        WriteScreenLog -Message "Error:Set item property path value"
    }     
  }
  if($LogFile)
  {
    Stop-Transcript 
  }  
}

 Bat 调用

ping 127.0.0.1 -n 6 > nul

powershell "D:\TimeJob\PDF.ps1" -WorkPlace "XXXXX" -MailTos "xxxxxxx,xxxxxxx"

ping 127.0.0.1 -n 2 > nul

 

posted @ 2016-11-03 15:10  batter152  阅读(870)  评论(0编辑  收藏  举报