zoukankan      html  css  js  c++  java
  • 使用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:SoftwarePDFCreatorProgram" 
    $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:TimeJobLog$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:TimeJobPDF.ps1" -WorkPlace "XXXXX" -MailTos "xxxxxxx,xxxxxxx"
    
    ping 127.0.0.1 -n 2 > nul
  • 相关阅读:
    openfl使用64位的ndk时,编译报错的问题!
    Haxe是何物?
    jsp中如何判断el表达式中的BigDecimal==0
    如何在springmvc的请求过程中获得地址栏的请求
    【原创】【滑块验证码】
    【原创】【aes加密】
    【原创】【qrcodejs2】生成二维码
    【原创】【ueditor】监听内容
    【原创】【ueditor】内容过多时 菜单控件遮挡页面
    js杂谈
  • 原文地址:https://www.cnblogs.com/batter152/p/6026707.html
Copyright © 2011-2022 走看看