使用 wkhtmltopdf 生成自动本地 PDF 文件名的批处理文件

Batch file for generating automatic local PDF filenames with wkhtmltopdf

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

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

start
cd C:\Program Files\wkhtmltopdf\bin
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/之后)article-may-2020-title然后本地保存的文件将自动生成或填充到批处理文件中,如“C:/Desktop/pdfs/article-may-2020-title.pdf”

这可以用批处理文件完成吗?使用 powershell 脚本更容易完成吗?如果有任何提示,我们将不胜感激。

谢谢。

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

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ProgramDirectory=%ProgramFiles%\wkhtmltopdf\bin"
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的program files目录

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

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

的程序文件目录中搜索 urls.txt

主命令行是 FOR 命令行,它处理 urls 列表文件中的所有 non-empty 行,其中包含一个空的字符串分隔符列表关闭默认的行拆分和没有行尾字符以真正处理 urls 列表文件中的所有 non-empty 行。

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

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

为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

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

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