'Batch file for using wkhtmltopdf to generate a single local PDF from a list of URLs
As described and solved by @Mofi in the previous question for individual files, I would like to use wkhtmltopdf to generate a single PDF file with the contents from a list of URLs within a text file.
The code below that Mofi provided works perfectly well for using the list of URLs to generate individual PDFs
@echo off
cd /D "%ProgramFiles%\wkhtmltopdf\bin" || exit /B if
for /F useback^ delims^=^ eol^= %%I in ("%ProgramFiles%\wkhtmltopdf\bin\urls.txt") do wkhtmltopdf.exe "%%~I" "%ProgramFiles%\wkhtmltopdf\bin\pdfs\%%~nI.pdf"
pause
I thought that by using the following code (by removing the loop from the pdf filename part) it would work. But it turned out that the PDF file kept rewriting itself with the article of the last URL it processed from the txt file.
@echo off
cd /D "%ProgramFiles%\wkhtmltopdf\bin" || exit /B if
for /F useback^ delims^=^ eol^= %%I in ("%ProgramFiles%\wkhtmltopdf\bin\urls.txt") do wkhtmltopdf.exe "%%~I" "%ProgramFiles%\wkhtmltopdf\bin\pdfs\master.pdf"
pause
What I need it do now, is just how to tweak this piece of code so as the batch file reads the URLs from the urls.txt file, there is a single PDF file generated which keeps growing in size by adding the contents of each URL into this single PDF.
Solution 1:[1]
@echo off
setlocal EnableDelayedExpansion
cd /D "%ProgramFiles%\wkhtmltopdf\bin" || exit /B if
for /F useback^ delims^=^ eol^= %%I in ("%ProgramFiles%\wkhtmltopdf\bin\urls.txt") do (
set "list=!list!"%%I" "
)
wkhtmltopdf.exe %list% "%ProgramFiles%\wkhtmltopdf\bin\pdfs\allFiles.pdf"
pause
This should work as long as the sites list does not exceed the max lenght for a variable (8192 characters). If the names have an average lenght of 60 characters, then the max number of sites is 136.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
