用于使用wkhtmltopdf自动生成本地PDF文件名的批处理文件 [英] Batch file for generating automatic local PDF filenames with wkhtmltopdf

查看:0
本文介绍了用于使用wkhtmltopdf自动生成本地PDF文件名的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的批处理文件,我想使用wkhtmltopdf创建一组存档URL的PDF文件。

我的wkhtmltopdf批处理文件的简单命令如下

start
cd C:Program Fileswkhtmltopdfin
start wkhtmltopdf.exe https://web.archive.org/web/20200524/website.org/article-may-2020-title"C:/Desktop/pdfs/file1.pdf"
pause

这在Windows 10环境中可以正常工作。因为它在上面的位置生成单个PDF文件,但文件名是您设置它的方式。

我想要实现的是从URL后面获取文章slug,并使其成为本地生成的PDF与文章slug具有相同的文件名;

即从上面的URL中取出部分(在.....WebSite[.]org/之后)文章-5-2020-标题,本地保存的文件将自动生成或作为";C:/Desktop/pdfs/article-may-2020-title.pdf";

填充到批处理文件中

可以使用批处理文件完成此操作吗?使用PowerShell脚本更容易做到这一点吗?如果是这样的话,任何提示都将不胜感激。

谢谢。

推荐答案

可以使用以下注释批处理文件:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ProgramDirectory=%ProgramFiles%wkhtmltopdfin"
set "OutputDirectory=%ProgramDirectory%pdfs"

set "ListFile=%~1"
rem Is the batch file started without any argument?
if not defined ListFile goto GetListFile

rem The batch file is started with an argument being interpreted as
rem file name of the urls list file which is checked for existence.
if exist "%ListFile%" for %%I in ("%ListFile%") do set "ListFile=%%~fI" & goto ProcessList
echo ERROR: File "%ListFile%" not found!& goto EndBatch

:GetListFile
rem Use urls.txt on existing in the current directory as urls list file.
if exist urls.txt for %%I in (urls.txt) do set "ListFile=%%~fI" & goto ProcessList

rem Use urls.txt in program files directory of wkhtmltopdf as urls list file.
if exist "%ProgramDirectory%urls.txt" set "ListFile=%ProgramDirectory%urls.txt" & goto ProcessList
echo ERROR: No file urls.txt found!& goto EndBatch

:ProcessList
rem Change the current directory to program files directory of wkhtmltopdf.
cd /D "%ProgramDirectory%" 2>nul
if errorlevel 1 echo ERROR: Directory "%ProgramDirectory%" does not exist!& goto EndBatch

rem Check the existence of program file wkhtmltopdf.exe.
if not exist "%ProgramDirectory%wkhtmltopdf.exe" echo ERROR: File "%ProgramDirectory%wkhtmltopdf.exe" not found!& goto EndBatch

rem Create the output directory and check if that is done successfully.
md "%OutputDirectory%" 2>nul
if not exist "%OutputDirectory%" echo ERROR: Failed to create directory "%OutputDirectory%"!& goto EndBatch

echo Processing the urls in file: "%ListFile%"
for /F useback^ delims^=^ eol^= %%I in ("%ListFile%") do "%ProgramDirectory%wkhtmltopdf.exe" "%%~I" "%OutputDirectory%\%%~nxI.pdf"

:EndBatch
endlocal
echo/
pause

wkhtmltopdf的程序文件目录在第三行中定义。

PDF文件的输出目录在第四行中定义。

可以使用参数启动批处理文件,该参数被解释为包含URL的文件的名称。否则,批处理文件将在当前目录中搜索名为urls.txt的文件,该文件可以是任何目录。最后在wkhtmltopdf的程序文件目录中搜索urls.txt

主命令行是for命令行,它使用空的字符串分隔符列表来处理URL列表文件中的所有非空行,以关闭默认的行分隔符,并且不使用行尾字符来真正处理URL列表文件中的所有非空行。

也可以使用"usebackq delims="而不是useback^ delims^=^ eol^=来处理URL列表文件中的所有行,行首带分号的URL除外。换句话说,在for命令行中使用"usebackq delims="时,可以用;行开头的;将列表文件中的URL注释掉。

每个url中最后/之后的字符串用作PDF文件的文件名。

要了解使用的命令及其工作原理,请打开command prompt窗口,在那里执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页。

  • call /?...说明%~1
  • cd /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

有关运算符&的说明,另请参阅single line with multiple commands using Windows batch file

这篇关于用于使用wkhtmltopdf自动生成本地PDF文件名的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆