使用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 "" |out-null
}
catch
{
new-itemproperty -path $PDFINFOPATH -name $USEAUTOSAVEPROPERTY -value "" |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 "" |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
使用PDFCreate 和 Powershell 自动保存网页为PDF的更多相关文章
- 使用 PowerShell 自动登录Azure
PowerShell 是管理 Azure 的最好方式之一,通过使用 PowerShell 脚本可以把很多的工作自动化.比如对于 Azure 上的虚拟机,可以设置定时关机操作,并在适当的时间把它开机,这 ...
- 一键保存网页为PDF
一键保存网页为PDF_V1.2 福利来了,“一键保存网页为PDF”发布以来最给力的功能来了: 1.支持大部分浏览器了(添加书签方式),测试IE8\IE9.谷歌.火狐可用: 2.转换组件“ephtm ...
- Azure 基础:用 PowerShell 自动登录
PowerShell 是管理 Azure 的最好方式,因为我们可以使用脚本把很多的工作自动化.比如把 Azure 上的虚拟机关机,并在适当的时间把它开机,这样我们就能节省一些开支,当然我们同时也为减少 ...
- 使用PowerShell 自动安装IIS 及自动部署网站
执行环境:Windows Server 2012 R2 安装iis核心代码,可自定义安装项 注意这里不能使用add-windowsfeature "Web-Filtering", ...
- Azure 基础:用 PowerShell 自动发布 CloudServices
在软件的开发过程中,自动化的编译和部署能够带来很多的优势.下面我们聊聊如何自动发布云应用程序到 azure 上的 cloud services. 打包要发布的内容 首先使用 msbuild 编译 *. ...
- 将HTML页面自动保存为PDF文件并上传的两种方式(一)-前端(react)方式
一.业务场景 公司的样本检测报告以React页面的形式生成,已调整为A4大小的样式并已实现分页,业务上需要将这个网页生成PDF文件,并上传到服务器,后续会将这个文件发送给客户(这里不考虑). 二.原来 ...
- 谷歌浏览器保存网页为pdf和.mhtml文件
注:需要安装chrome谷歌浏览器,尽可能安装最新版的浏览器: 一:保存为pdf 打开要加载的网页 空白处右键-----打印 配置要打印的pdf网页 点击 左边----> 保存按钮 即可: ...
- 使用PowerShell自动部署ASP.NetCore程序到IIS
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...
- PowerShell自动部署ASP.NET Core程序到 IIS
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...
随机推荐
- tomcat6类加载器与类加载顺序
tomcat6.0.32 com.dyyx.ShareUtils//返回系统当前时间public static String now(); package com.dyyx;import java.t ...
- [New Portal]Windows Azure Virtual Machine (18) Azure Virtual Machine内部IP和外部IP
<Windows Azure Platform 系列文章目录> 在开始本章内容之前,请读者熟悉以下2篇博文: [New Portal]Windows Azure Virtual ...
- oracle数据库字符集US7ASCII,在java中处理中文问题
原来项目中oracle数据库一直是US7ASCII,我新项目对接的时候,查询以及插入中文,出现乱码问题. 暂时未能解决此问题,最终决定每次转码: 查询的时候: List<Record> l ...
- CNN笔记
Deep Learning是全部深度学习算法的总称,CNN是深度学习算法在图像处理领域的一个应用. 转 http://blog.csdn.net/stdcoutzyx/article/details/ ...
- React-Native 给客户端来个「同音词模糊搜索」
APP上线一段时间有用户反应说不方便找东西,其实APP的数据不大也就三四百条而以,但受限于手机端展示区域太小.信息大爆炸,大家也基本上不会去记具体的名称都是根据模糊的印象进行搜索而且现在大家基本都用拼 ...
- ios基础之 view的frame 与 bounds 的区别 (转)
前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...
- [译]学习IPython进行交互式计算和数据可视化(六)
第五章:高性能并行计算 一个反复被提及的反对使用Python进行高性能数值计算的言论是这种语言是动态解释型的,速度太慢.一种编译型低级语言,如C,能提供比它快几个数量级的运算速度.我们在第三章--使用 ...
- C#:Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- CSS代码重构与优化之路
作者:@狼狼的蓝胖子 网址:http://www.cnblogs.com/lrzw32/p/5100745.html 写CSS的同学们往往会体会到,随着项目规模的增加,项目中的CSS代码也会越来越多, ...
- PHP条件语句语法与示例
一.if…else语句 语法: 1 if(条件){ …… } else{ …… } 2 if(条件){ …… } elseif(条件){ …… } else{ …… } 示例1: <?php & ...