Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating PDF from webpage

I have my resume online in an html page (www.mysite.com/resume.html). Every time I need a PDF version of it, I use Google Chrome's print dialog to save it as a PDF, which uses my print CSS stylesheet.

I want to be able to navigate to www.mysite.com/resume.pdf to always have an up to date PDF version without having to go through Google Chrome manually. Is there a way to programmatically and automatically create a resume.pdf from resume html? If I can write a script that runs once every 24 hours or something like that, that would be good.

like image 575
russtuck91 Avatar asked May 28 '13 23:05

russtuck91


People also ask

How do I save a webpage as PDF programmatically?

Save Webpage as PDF File in C# You can quickly save a webpage to PDF format with the following steps: Load the input webpage by specifying its URL. Create an object of the PdfSaveOptions class. Save the webpage as a PDF file with ConvertHTML method.

How do I create a PDF from a Web page?

The fastest way to convert an open web page to PDF is to use the Adobe PDF toolbar. If you have the toolbar installed in your browser, simply select the Convert To PDF tool. Give your new PDF file a name, select Save, and voila — the conversion is done.

How do I create a PDF from HTML?

On a Windows computer, open an HTML web page in Internet Explorer, Google Chrome, or Firefox. On a Mac, open an HTML web page in Firefox. Click the “Convert to PDF” button in the Adobe PDF toolbar to start the PDF conversion. Enter a file name and save your new PDF file in a desired location.


2 Answers

Chrome has started headless program.

With that, we can create a pdf. e.g. for windows navigate your commandline to

C:\Users\{{your_username}}\AppData\Local\Google\Chrome SxS\Application>

Then hit the command:

chrome --headless --print-to-pdf="d:\\{{path and file name}}.pdf" https://google.com
like image 63
Vikas Avatar answered Oct 06 '22 00:10

Vikas


PhantomJS is perfect for this. It invokes an instance of WebKit from the command line which can then be used to output to file such as PDF.

PhantomJS:

http://phantomjs.org/

Specific instructions for exporting screen output to a file:

http://phantomjs.org/screen-capture.html

e.g.:

phantomjs rasterize.js 'http://www.example.com/resume.html' resume.pdf
like image 41
tommypyatt Avatar answered Oct 05 '22 23:10

tommypyatt