-1

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
valdroni
  • 158
  • 3
  • 18
  • 3
    That is all dependent on the functionality of the software you are using to create the pdf. No amount of batch file code will solve this issue. Your software has to have an option to append to an existing pdf file – Squashman Feb 24 '22 at 19:34
  • It does have an option for appending to an existing PDF file. The command is as follows `start wkhtmltopdf.exe https://web.archive.org/web/20200524/website.org/article-1 https://web.archive.org/web/20200524/website.org/article-2 "C:\Program Files\wkhtmltopdf\bin\master.pdf"` As you may see, after the .exe call you may put the URLs one after the other and then define the location of the PDF file where they get created/appended to. How to use the logic/batch file command from the code provided in OP to make this work? – valdroni Feb 24 '22 at 20:03

1 Answers1

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • It didn't work with the setlocal variable as the command prompt window just flashed up and dissapeared. When removing it and using the following code, it only generates the PDF with the first URL and then stops (with message: Press any key to continue...). The code I used `@echo off 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 --header-center %list% "%list%" "%ProgramFiles%\wkhtmltopdf\bin\pdfs\allFiles.pdf" pause` – valdroni Feb 24 '22 at 21:25
  • **1.** My code works as it is written, you may test it if you insert an `echo The list: %list%` command between the right paren and the `wkhtmltopdf.exe` command. You may open a `cmd.exe` window and run the code there, so you can review in the screen what happened (don't run it via a double-click). If you remove the `setlocal` command, it don't works anymore. You may test this in the same way. **2.** Why you inserted the list twice in your command? `wkhtmltopdf.exe --header-center %list% "%list%"` (one time as is, and another time between quotes). In _my code_ there is just one time! – Aacini Feb 24 '22 at 23:12
  • Don't want you a code that solve your problem? Or you are trying to make my solution fail in several different ways... **`:(`** – Aacini Feb 24 '22 at 23:13
  • Lol, no I am not trying to fail your code, I appreciate your help. This part is from the application syntax as it prints the URL ( from your "%list%") on the header of PDF as I needed it to be there `--header-center "%list%"`. I will try it again soon and give feedback. – valdroni Feb 25 '22 at 08:25
  • You said that _"after the .exe call you may put the URLs one after the other and then define the location of the PDF file where they get created/appended to"_, so I think that the command should be `wkhtmltopdf.exe %list% --header-center "%list%" "%ProgramFiles%\wkhtmltopdf\bin\pdfs\allFiles.pdf"`, isn't it? Note that my code encloses in quotes _each URL_; if you want not this, remove the quotes from `"%%I"` above. – Aacini Feb 25 '22 at 17:07
  • So, taking your example URL's from a previous comment, would you want to run this command: `wkhtmltopdf.exe https://web.archive.org/web/20200524/website.org/article-1 https://web.archive.org/web/20200524/website.org/article-2 --header-center "https://web.archive.org/web/20200524/website.org/article-1 https://web.archive.org/web/20200524/website.org/article-2" "C:\Program Files\wkhtmltopdf\bin\master.pdf"`? That looks strange for me, but I'm not the one that know how to use this application... **`:(`** – Aacini Feb 25 '22 at 17:07
  • Apologies for late reply. The command would have to be like: `wkhtmltopdf.exe https://archive.org/web/2020/web.org/article-1 https://archive.org/web/2020/web.org/article-2 https://archive.org/web/2020/web.org/article-3 "C:\Program Files\wkhtmltopdf\bin\master.pdf"` Note that I removed the --header-center string (not needed anymore). So, I need your loop to generate all the URLs from txt file one after the other (in this example: article-1 article-2 article-3), so all these URLs are processed from wkhtmltopdf.exe and generate a single PDF file in `"C:\Program Files\wkhtmltopdf\bin\master.pdf"` – valdroni Mar 01 '22 at 14:39