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

问题描述 投票:0回答:1

我有一个简单的批处理文件,我想使用它来使用 wkhtmltopdf 创建一组已存档 URL 的 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 脚本可以更轻松地完成此操作吗?如果是这样,我们将不胜感激。

谢谢。

batch-file batch-processing wkhtmltopdf
1个回答
2
投票

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

@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的程序文件目录。

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

批处理文件可以使用一个参数启动,该参数被解释为包含 url 的文件的名称。否则,批处理文件会在 当前目录 中搜索名称为

urls.txt
的文件,该文件可以是任何目录。最后在
wkhtmltopdf
的程序文件目录中搜索urls.txt

主要命令行是 FOR 命令行,它处理 urls 列表文件中的所有非空行,并使用空的字符串分隔符列表来关闭默认的行分割,并且没有行尾字符来真正处理所有非空行。 - url 列表文件中的空行。

也可以使用

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

每个 url 中最后一个

/
之后的字符串用作 PDF 文件的文件名。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

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

另请参阅 使用 Windows 批处理文件的单行多个命令,了解运算符的说明

&

© www.soinside.com 2019 - 2024. All rights reserved.