9

HTML to PDF JavaScript – Easily Convert Web Pages to PDF

 3 years ago
source link: https://www.simplifiedcoding.net/html-to-pdf-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

HTML to PDF JavaScript – Easily Convert Web Pages to PDF

Hey guys, today I have HTML to PDF JavaScript Tutorial for you all. Sometimes we need to give our users an option to download documents. You may need this feature for several things; for example, you need to let your users download Invoices, Reports, Charts in PDF Format. Or sometimes, you may even need to let your users download an existing HTML page to PDF Documents. Whatever may be your requirement, the primary thing required here is converting HTML to PDFs. And in this HTML to PDF JavaScript Tutorial, I am going to teach you this thing.

Once you know how you can convert HTML to PDF, you can apply it to all your requirements discussed above.

In this post I will create a very simple web app that will take WebPage URLs, and give you PDF Document by converting them to PDF. So basically we will be using JavaScript.

HTML to PDF JavaScript

The tool I will be using to make it is called DocRaptor. It is a paid tool, but you can get a Free Trial, and you can convert Unlimited Documents for Free of you do not have any problem with WaterMarked Docs. 😉

If you want to know in advance what exactly we will build in this post, check it below.

I have embedded it here using IFrame, you can check Web 2 PDF Here.

Now, let’s start building it.

Tools Required

Following are the things, that I have used to build that application.

  • DocRaptor – For converting HTML to PDF
  • VisualStudio Code – As my code editor (You can use any)
  • Firebase Hosting – To deploy the application

Building an HTML to PDF Converter

Now, let’s build our project. Basically you just need to create a Folder and it is our project.

  • Create a folder named public inside your project folder.
  • Inside the public folder create an HTML file, you can name it anything; but I will recommend naming it index.html
  • Now let’s design the UI for our tool. I have used Bootstrap here.
<!DOCTYPE html>
<html>
    <!--
        HTML to PDF JavaScript Tutorial using DocRaptor
        www.docraptor.com
    -->
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    </head>
    <body>
        <div class="container">
            <div style="margin-top:15%">
                <div class="d-flex justify-content-center">
                    <h1>Web 2 PDF</h1>
                </div>
            </div>
            <div>
                <div class="d-flex justify-content-center">
                    <h4>Easily Convert HTML to PDF and other formats</h4>
                </div>
            </div>
            <div>
                <div class="d-flex justify-content-center">
                    <h5>Powered By <a href="https://docraptor.com/">DocRaptor</a></h5>
                </div>
            </div>
            <div>
                <div class="d-flex justify-content-center">
                    <div class="form-group" style="width: 100%">
                        <label for="url">URL</label>
                        <input type="url" id="url" placeholder="enter url and convert to pdf" class="form-control form-control-lg" />
                    </div>
                    <br />
                </div>
                <div class="d-flex justify-content-center">
                    <div class="form-group">
                        <button id="btnConvertToPDF" class="btn btn-primary btn-lg">Get PDF</button>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
  • The UI is very simple, we just have an Input Field to get a WebPage URL and then we will convert the given page to PDF.
  • For converting the URL to PDF we will write some JavaScript; I am using jQuery here to simply my JavaScript code.
        <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
        <script src="https://docraptor.com/docraptor-1.0.0.js"></script>
        <script>
            $("#btnConvertToPDF").click(function(){
                downloadPDF();
            var downloadPDF = function() {
                var url = $("#url").val();
                if(url == ""){
                    alert("Enter a URL")
                    return;
                DocRaptor.createAndDownloadDoc("API KEY", {
                    test: true, // test documents are free, but watermarked
                    type: "pdf",
                    document_url: url
        </script>
  • As you can see I have added DocRaptor JS and jQuery and the rest of the code is absolutely simple.

Not only PDF you can convert the page to other formats as well by changing the type value. Currently it is PDF as we want PDF.

And it is not necessary to pass a web url all the time, you can also pass complete HTML string instead of url. To pass an HTML String we will do it like this.

document_content: "<html></html>"

If you will pass test: true , that means it is free for you, and you will get WaterMarked document.

Deploying your Project

Finally, if you want to deploy your tools for Free, you can use Firebase Hosting as I did. If you want to check a Step by Step guide about using Firebase Hosting then you can check it here.

So that is all for this tutorial friends, let me know if you have any question regarding this HTML to PDF JavaScript Tutorial. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK