1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best way to create PDF using PHP on the fly?

Discussion in 'PHP' started by ahkip, May 12, 2006.

  1. #1
    I have a PHP program and i would like to add fucntion to create a PDF file of my invoice page so that user can download their invoice.

    I did google search and found some extension. However, they can's read CSS and very hard to create any format.
    SEMrush
    If you used any program that work with CSS, please let me know. Thx in advance
     
    ahkip, May 12, 2006 IP
    SEMrush
  2. mad4

    mad4 Peon

    Messages:
    6,989
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    CSS is client side. You need to create the pdf document server side and output it to the browser as a pdf document.

    http://www.fpdf.org/ is the best way to do this on an apache server. Its free as well.
     
    mad4, May 12, 2006 IP
  3. Xig

    Xig Peon

    Messages:
    75
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, as Mad4 says I think you're talking about two different things there.

    If you are generating an HTML invoice that completely uses CSS, you can generate a separate CSS file for printing so the browser will automatically format it correctly for the page. A good tutorial for that can be found here:
    http://www.alistapart.com/articles/goingtoprint/

    Alternatively you can use server-side software to generate a PDF file, which is then downloadable by the client. I use html2pdf for my invoicing - it works very well and reads in HTML/CSS pages (is that what you meant?) for conversion to PDF. It can be found here: http://html2pdf.seven49.net/
     
    Xig, May 12, 2006 IP
    ahkip likes this.
  4. ahkip

    ahkip Prominent Member

    Messages:
    9,206
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #4
    Thx Mad4, you are always helpful :)
    i tried http://www.fpdf.org/ but it doesn't work with CSS

    let me try html2pdf Xig, may be the worst i have to go back to HTML...
     
    ahkip, May 12, 2006 IP
  5. mad4

    mad4 Peon

    Messages:
    6,989
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Its not supposed to work with CSS. Trying to use CSS in a pdf document is like using javascript in a Word document. CSS is a language for use in your internet browser rather than Adobe Acrobat Reader.

    If you are creating the invoice using php then you need to use fpdf to generate the output as a pdf document rather than a html document.
     
    mad4, May 12, 2006 IP
  6. Xig

    Xig Peon

    Messages:
    75
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    *laughs* Good analogy ...

    AFAIK the html2pdf software parses HTML and CSS to create a PDF document. It's only using HTML and CSS to read in formatting, layout, colours etc and then it generates the PDF document from scratch. I guess its an HTML/CSS -> PDF converter.

    Or did I misunderstand your post?
     
    Xig, May 12, 2006 IP
  7. ahkip

    ahkip Prominent Member

    Messages:
    9,206
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #7
    html2pdf is cool, but it is too expensive for me:(
     
    ahkip, May 12, 2006 IP
  8. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #8
    http://www.fpdf.org/ is a sound extension just extend the class to include the css properties that you want, i say css properites in the basics term meaning setting a basic formatted layout. The just add the invoice data on top.
     
    Big 'G', May 12, 2006 IP
  9. calvinmicklefinger

    calvinmicklefinger Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I know I'm joining this thread late, and to add to that, I'm a complete doofus with coding ... but I have to learn this, so my question is ...

    What can I use to lay out and plan a page for conversion to PDF with the use of fpdf?

    How do I figure out what goes where and send that to fpdf?

    Thanks in advance.
    Kirk Ward
     
    calvinmicklefinger, Jan 5, 2008 IP
  10. mad4

    mad4 Peon

    Messages:
    6,989
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #10
    fpdf has some good documentation you can follow.
     
    mad4, Jan 6, 2008 IP
  11. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #11
    Good info. Do we need to install anything for FPDF?
     
    kks_krishna, Apr 12, 2008 IP
  12. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #12
    when I try to convert the HTML tags, it is not converting properly.
     
    kks_krishna, Apr 13, 2008 IP
  13. TheSyndicate

    TheSyndicate Prominent Member

    Messages:
    5,368
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    365
    #13
    I would like to use this method to print out an ebook with the email of the buyer on it. Is that possible? It would be good to avoid people selling the book on the internet or giving it away.
     
    TheSyndicate, Apr 15, 2008 IP
  14. Yankee85

    Yankee85 Peon

    Messages:
    1,072
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
  15. zeckdude

    zeckdude Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I'm having some problems with the FPDF class. I'm hoping you can help me. I'm using FPDF to write some text on a PDF. I'm displaying 8 different cells of text. The Cells are being displayed, but after two cells, it puts the next cell onto a new page as If I had asked for a new PDF page to be made, but I did not. I'm guessing it is somehow pushing them down and pushing it onto the next page. How can I get rid of these annoying spaces and make it all fit on only the one PDF page?

    Here's the code:

    <?php
    // Get required files.
    require 'fpdf/fpdf.php';
    
    // Set some document variables
    $author = "Me McMe";
    $company_name = "APPLIED MATERIALS";
    $name = "Liang Y. Chen, Ph.D.";
    $title = "Corporate Vice President & General Manager";
    $title_2 = "Alternative Energy Products Group";
    $dept_div = "Energy & Environmental Solutions";
    $phone = "T  408.563.6330";
    $fax = "F  408.235.6989";
    $email = "Liang_Chen@amat.com";
    $address2 = '3535 Garrett Drive, M/S 10071fdsfsdf';
    
    // Create fpdf object
    $pdf = new FPDF('L', 'mm', array(51,87)); //fpdf(orientation(L for Landscape), unit of measurement(mm for millimeters), 
    
    // Add a new page to the document
    $pdf->addPage('L', array(51,87));
    
    $pdf->SetDisplayMode(real,'two');
    $pdf->SetFont('Arial','B',5);
    $pdf->SetTextColor(0,0,0); 
    $pdf->SetXY(0, 0);
    $pdf->SetMargins(0,0,0);
    
    $pdf->Cell(0,11, "$company_name", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$name", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$title", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$title_2", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$dept_div", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$phone", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$fax", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$email", '1', 2, 'L', false);
    $pdf->Cell(0,11, "$address2", '1', 2, 'L', false);
    
    $pdf->Image('amatlogo.gif', 0, 0, 23.28, 21.52, 'GIF');
    $pdf->Output('simple.pdf','I');
    ?>
    Code (markup):

    You can view the outputted pdf here: http://idea-palette.com/pdf/
     
    zeckdude, Sep 30, 2009 IP
  16. silverf0x

    silverf0x Active Member

    Messages:
    535
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    78
    #16
    I saw the PDF and it all fit in one page.
     
    silverf0x, Apr 16, 2011 IP
  17. Zetiz

    Zetiz Active Member

    Messages:
    668
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #17
    FPDF is a nice PHP class to generate pdfs using PHP.
     
    Zetiz, Apr 16, 2011 IP
  18. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #18
    I haven't tried, but it looks great: http://www.tcpdf.org/examples.php (example 61, XHTML + CSS)

    I have worked with fpdf. Here is example: http://demo.dalibor-sojic.info/_testpdf/test.php (fill in first 3 fields) and submit the form.

    Here is t.php:
    
    <?php	
    	require_once('fpdf16/fpdf.php');
    	require_once('fpdi/fpdi.php');
    	
    	require_once('fields.php'); // field properties
    	
    	$pdf =& new FPDI();
    	
    	
    	$pagecount = $pdf->setSourceFile('template.pdf');
    	$tplidx = $pdf->importPage(1); 
    	
    	$pdf->SetTitle('Test pdf');
    	$pdf->SetAuthor("Me");
    	$pdf->SetSubject("Light Savings");
    	
    	
    	$pdf->addPage(); 
    	$pdf->useTemplate($tplidx, 0, 0); 
    	
    	
    	$pdf->SetFont('Courier', 'B', 10);
    	
    	
    	// Show Data
    
    	
    	$pdf->SetXY(150, 10);
    	$pdf->Cell(35, 5, date('m/d/Y'), 0, 0, 'R');
    
    	
    	
    	foreach($fields as $field=>$position) {
    		$pdf->SetXY($position['x'], $position['y']);
    		$pdf->Cell($position['width'], 4, $_POST[$field], 0, 0, $position['aligment']);
    	
    	
    	}
    	
    	$pdf->Output('result.pdf', 'I');
    
    
    PHP:
     
    Last edited: Apr 17, 2011
    sojic, Apr 17, 2011 IP
  19. silv3r.m00n

    silv3r.m00n Active Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #19
    silv3r.m00n, Dec 2, 2011 IP