Jump to content

Converting a webpage to PDF on the fly


soonguy

Recommended Posts

Hi

 

I want to offer users the option of saving a particular webpage report as a PDF file. (Our server is running PHP 4.4.7) I've looked at several of the options available, and find them fiendishly complicated, or have not managed to get them to work. A friend gave me some simple PHP code to try. The following does convert the page to PDF as soon as it is opened in the browser - which is not want I want. Plus, it just puts all the html code for the page into the PDF:

<?php
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Tips</title>
</head>
<body>
Testing the pdf converter
</body>
</html>
<?php
# grab the HTML above as a string from the buffer
$html = ob_get_contents();
ob_end_clean(); # discard the buffer - we have it already in $html and don't want to output it to the browser - this would send headers and cause errors.

# now the pdf
include( 'class.ezpdf.php' );
$pdf = new Cezpdf( 'a4', 'P' );  //A4 Portrait
$pdf -> ezSetCmMargins( 2, 1.5, 1, 1);
$pdf->selectFont( '.Helvetica.afm' );
$pdf->ezText( $html, 10 );
$pdf->ezStream();
?>

 

What I really want is a button that will convert the page, iwhen someone wants to. And, ideally, will just offer to save the PDF as a file, rather than opening it in Acrobat.  So I tried the following, but it does not work:

 

<?php
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Tips</title>
</head>
<body>
<form action="pdftest7.php" method="get">
Testing the pdf converter
<input type="submit" value="save this page as pdf">
</form>
</body>
</html>
<?
$html = ob_get_contents();
ob_end_clean(); 
if (array_key_exists('pdf', $_GET)) {
  include( 'class.ezpdf.php' );
  $pdf = new Cezpdf( 'a4', 'P' );  //A4 Portrait
  $pdf -> ezSetCmMargins( 2, 1.5, 1, 1);
  $pdf->selectFont( './Helvetica.afm' );
  $pdf->ezText( $html, 10 );
  $pdf->ezStream();}
else {
  echo $html;
}
?>

 

 

Thanks for any wisdom on this!

 

Best wishes

 

Tony

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.