Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Uncategorized»Convert HTML to PDF using Qtwebkit with PHP

    Convert HTML to PDF using Qtwebkit with PHP

    RahulBy RahulJune 21, 20131 Min Read

    Wkhtmltopdf is a very useful application to create pdf from html (webpage). This article will help to create pdf of a webpage using php script and Linux command line tool.

    Step 1: Install wkhtmltopdf in Linux

    Download wkhtmltopdf from google code and install to linux system.

    # cd /opt
    # wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
    # tar xjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
    # mv wkhtmltopdf-i386 /usr/bin/wkhtmltopdf
    # chown apache:apache /usr/bin/wkhtmltopdf
    # chmod +x /usr/bin/wkhtmltopdf
    
    Step 2: Create Pdf Using Command Line

    First check wkhtmltopdf script it its properly working from command line. Below command will create pdf of http://google.com web page.

    # /usr/bin/wkhtmltopdf http://google.com google.pdf
    
    Step 3: PHP Code to Create Pdf Using wkhtmltopdf

    Use below block of php code to generate pdf from html ( webpage ). This script required to be enabled shell_exec function for Apache. Most of shared hosting provides doesn’t allow this function.

    Create a file name getPdf.php using below code and put it on your website document root

    PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?php
     
    $url = $_GET['url'];    // Website URL to Create pdf
    $name = $_GET['pdf'];   // Output pdf name
     
    $command = "/usr/bin/wkhtmltopdf ";
    $pdf_dir = "/var/www/html/pdfs/";     // Pdf files will be saved here
    $ex_cmd = "$command $url " . $pdf_dir . $name;
    $output = shell_exec($ex_cmd);
     
    ?>

    Open Following url to generate pdf of website ( html ).

    Syntax:
    http://youdomain.com/getPdf.php?url=<website url>&pdf=<pdf name>

    Example:
    https://tecadmin.net/getPdf.php?url=http://google.com&pdf=google.pdf


    html to pdf Qtwebkit Wkhtmltopdf
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Find My Public IP Address from Linux Command Line
    Next Article ffmpeg: error while loading shared libraries: libavdevice.so.55: cannot open shared object file: No such file or directory

    Related Posts

    Most Effective Industry Specific Plug-ins for WordPress

    Updated:August 15, 20163 Mins Read

    How to Install Komodo Edit on Ubuntu 14.10, 14.04 LTS and 12.04 LTS via PPA

    1 Min Read

    Setup Database Mirroring in SQL Server 2012 with Certificates

    3 Mins Read

    How to Install XCache for PHP on CentOS,RHEL and Fedora

    2 Mins Read

    Setup Varnish on CentOS/RHEL 5/6 with Apache

    2 Mins Read

    Error in query (1548): Cannot load from mysql.proc. The table is probably corrupted

    1 Min Read

    1 Comment

    1. b1ack1323 on August 18, 2014 1:10 pm

      Will this work in CentOS5?

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.