Converting a webpage to PDF on the fly

Discussion in 'PHP' started by soonguy, Oct 24, 2007.

  1. #1
    Hi

    I am trying to implement the simplest PHP on-the-fly html to pdf conversion I can find, which seems to be the one written up quite logically at
    www.zend.com/zend/spotlight/creatingpdfmay1.php

    Using the code supplied on a test page, it gives
    Fatal error: Call to undefined function: pdf_new() in /var/www/ied/pdftest3.php on line 9
    for the very first line of code.
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body> 
    Testing a pdf converter
    <?php
    $pdf = PDF_new();
    PDF_open_file($pdf); 
    PDF_set_info($pdf, "author", "John Coggeshall"); 
    PDF_set_info($pdf, "title", "Zend.com Example"); 
    PDF_set_info($pdf, "creator", "Zend.com"); 
    PDF_set_info($pdf, "subject", "Code Gallery  Spotlight");
    PDF_begin_page($pdf, 450, 450); 
    $font = PDF_findfont($pdf, "Helvetica-Bold",  "winansi",0); 
    PDF_setfont($pdf, $font, 12);
    PDF_show_xy($pdf, "Hello, Dynamic PDFs!", 5, 225);
    PDF_end_page($pdf);
    PDF_close($pdf);
    $buffer = PDF_get_buffer($pdf);
    header("Content-type: application/pdf");
    header("Content-Length: ".strlen($buffer));
    header("Content-Disposition: inline; filename=zend.pdf");
    echo $buffer; 
    PDF_delete($pdf); 
    ?>
    </body>
    </html>
    
    PHP:
    Anyone know what I am doing wrong, and also how to actually use the script and create a 'save as PDF' button - ideally which will give a file save option, rather than just opening the converted page in Acrobat? Presumably just a form enclosing all the content you want to convert?

    I am also unsure if the PHP should start after the close of the html tag on the page.

    Many thanks

    Tony
     
    soonguy, Oct 24, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi soonguy

    I am guessing that the pdf library has not been compiled into the php that you are running - www.php.net says that it is not included by default. Have a look here: http://uk2.php.net/manual/en/ref.pdf.php

    Also, you will need to take out all of the html header information - for the header() function to work, you cannot output any data before you call this function.

    Brew
     
    Brewster, Oct 24, 2007 IP
  3. webrepair

    webrepair Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Brewster is correct. You need to compile the library. You should also make sure you read the document reference on those functions to ensure (for example), that there is no HTML output before you echo you generated pdf file.
     
    webrepair, Oct 24, 2007 IP
  4. undir

    undir Peon

    Messages:
    696
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I can help you out please msg me
     
    undir, Oct 24, 2007 IP
  5. soonguy

    soonguy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi

    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();
    ?>
    Code (markup):
    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;
    }
    ?>
    
    Code (markup):

    Thanks for any wisdom on this!

    Best wishes

    Tony
     
    soonguy, Oct 26, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I'm not sure if this is possible as the browser behaviour is depended on the settings on the client PC. I would suggest having a link to the file that creates the pdf output and telling the user to right click and select "save as" from the menu.

    Brew
     
    Brewster, Oct 26, 2007 IP
  7. bLuefrogx

    bLuefrogx Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I believe you want
    
    header("Content-Disposition: inline; filename=zend.pdf");
    
    PHP:
    Instead of inline ;)
     
    bLuefrogx, Oct 28, 2007 IP
  8. soonguy

    soonguy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    THanks, I am still a bit confused!
    The following coding will indeed convert to PDF when you click the button. But it converts all the html on the page including the header and all the <> etc.

    What of course I need it to do is convert only the body code, and strip out <> and deal with it according, eg <p> to become linebreak, <h> to be larger, etc

    thanks for any ideas

    tony
    <?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
    <h4>Headline</h4>
    some more
    some more
    
    <input name="pdf" 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;
    }
    ?>
    
    PHP:
     
    soonguy, Oct 28, 2007 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    You need to use a commercial converter.

    All the free ones are only good for text and small tables, more complex sites will not work.

    Peace,
     
    Barti1987, Oct 28, 2007 IP