You are here:Home » Script » Create PDF From Page

Create PDF From Page


In this tutorial, i am going to teach you how to print page as pdf. Simple, just follow these steps.I will be using the CrowdPDF API. So you are required to signup with them and obtain an API key. Next, you would edit the setting on this script to reflect the your API key. The required crowd crowdPDF file is included in the folder attached. Hope you would add some comments.

Download

<?php
require 'pdfcrowd.php';
function generatePDF()
{
    if (!$_GET["action"])
        return False;

    try {
        // build the url and remove the pdf field from the query string
        $url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
        if (count($_GET) > 1) {
            unset($_GET["action"]);
            $url = $url . "?" . http_build_query($_GET, '', '&');
        }

        // call the API enter your username, and API key
        //you must register at pdf crowd.com to get this key
        $client = new Pdfcrowd("username", "apikey");
        $pdf = $client->convertURI($url);

        /*
        send the generated pdf to the browser
        you will see a save as option because of this headers.
        Alternatively, if you prefer to open for viewing,
        copy to your server ie copy($pdf, $destination), and use header("Location: pdf_link.php") to open
        */
   
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"createdFile.pdf\"");
    
        echo $pdf;
    }
    catch(PdfcrowdException $why) {
        echo "PDF creation failed: ".$why."\n";
    }

    return true;
}

if (isset($_GET['action'])=='print'){
    generatePDF();
}
//visit http://pdfcrowd.com/web-html-to-pdf-php/ for more info
?>
<html>
<head>
<title>Print HTML Page to PDF DOC - by Ohwofosirai Desmond</title>
</head>
<body>
<h1>HTML Page 2 PDF</h1>
<p>
You are viewing your website page in form of a pdf doc.Following the Instructions by Ohwofosirai Desmond.
</p>
Be aware Desmond has a social network software application for you at http://23cliques.com. Just go on check the demo
<br/>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="hidden" name="action" value="print"/>
<input type="submit" value="Print as PDF"/>
</form>
</body>
</html>

html2pdf, page to pdf, print page as pdf

0 comments:

Post a Comment