Saturday, May 26, 2018

Printing web pages to pdf

As an extremely lazy person I started automating a lot of tasks... so many that I started writing the results (successful execution / failed execution) into the database to query for errors. Then I started writing the logs and execution times into db. It became so huge that we wrote a dashboard application to check state.

Today I want to forward this dashboard to some colleagues without granting them access to the platform... solution: mailing a pdf. I asked myself if there is a way to convert the html page into a pdf easily and found the following solution with chrome. The following snippets shows a batch script for windows.

@echo  %time%
@"C:\Users\{user}\AppData\Local\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --print-to-pdf=c:\{outputFolder}\output.pdf file:///{pathToIndex}/index.html
@echo  %time%

Keep an eye on the fact that the screenshot is taken after onload is executed which can take several seconds.

No comments: