dhtml2pdf is a simple, free and very easy to use PHP API that allows you to see, download or get the binary of the PDF generated from the HTML of an URL.
It uses the snappy image & PDF from URL generation PHP library, which is based in the awesome webkit-based wkhtmltopdf and wkhtmltoimage CLI.
Try it out here! 👉 https://dhtml2pdf.herokuapp.com/
As I'm using the free Heroku containers to host the app, so it sometimes stops working because of the quota limits they have. I've created a second instance of the app (👉https://dhtml2pdf2.herokuapp.com/) which you can use, however I recommend you set up your own private Heroku instance if you plan to frequently use this
Currently, the API is an PHP-based end point which simply allows you to pass as parameter the URL of the HTML page that you want to convert to PDF. It's deployed as a Heroku APP so you can use it whenever you want to.
It's as easy as this:
https://dhtml2pdf.herokuapp.com/api.php?url=<your_url>&result_type=<result_type>
Params:
url
. The URL of the site you want to convert to PDF. Example:url=https://www.github.com
result_type
. The way you want to retrieve the generated PDF. Can be one of the following:show
. Opens the generated PDF in the browser.download
. Downloads the generated PDF.binary
. Returns the binary content of the generated PDF.
file_name
. If you choosedownload
in theresult_type
parameter, this is the name of the file that will be downloaded (you must pass the name) without the .pdf extension.
Example:
This:
https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show
would open the generated PDF from the https://www.github.com
site in your browser.
Anchor to show the PDF in a new browser tab:
<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show" target="_blank">Show PDF</a>
Anchor to download the PDF as my_pdf.pdf:
<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=download&file_name=my_pdf" target="_blank">Download PDF</a>
PRO TIP: Show or download current page in PDF
<a href="javascript:window.open('https://dhtml2pdf.herokuapp.com/api.php?url='+window.location.href+'&result_type=show', '_blank')" target="_blank">Show PDF</a>
Retrieve the binary data of the PDF:
$.ajax({
type: "GET",
url: "https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&&result_type=binary",
success: function(data){
//Prints the PDF binary data in the browser console
console.log(data);
},
error: function(err) {
console.log(err);
}
});
If you don't want to depend on a external service as this one, you can easily clone the repo and deploy it in your own server. I will show how to deploy it in a Heroku server as it's easy and fast to install and free!
Clone the repo:
git clone https://github.com/Dellos7/dhtml2pdf.git
cd dhtml2pdf
If you had any troubles following the below instructions, please visit the Heroku PHP getting started guide at https://devcenter.heroku.com/articles/getting-started-with-php#set-up
Sign up in Heroku: https://signup.heroku.com/
Download & install Heroku CLI: https://devcenter.heroku.com/articles/heroku-cli#download-and-install
Login in the Heroku CLI:
heroku login
Create your Heroku APP:
heroku create <your_app_name>
(your APP name will be deployed then in https://<your_app_name>.herokuapp.com)
Update & create the composer dependencies:
composer update
Remove the current git repository and create a Heroku one:
rm -r .git
git init
git remote add heroku https://git.heroku.com/<your_app_name>.git
git add .
git commit -m "first commit"
We only need a last command in order to push our APP to heroku, which will perform the PHP build and deploy the APP!
git push -u heroku master
Working currently on
heroku-16
stack andheroku-18
stack