PDF Export in PHP

PDF Export in php

If your intention is to create a PDF from PHP, pdflib will help you.

Otherwise, if you want to convert an HTML page to PDF via PHP, you'll find a little trouble outta here.

So, the options I know are:

DOMPDF : PHP class that wraps the HTML and builds the PDF. Works good, customizable (if you know PHP), based on pdflib, and, if I remember correctly, it takes even some CSS. Bad news: slow when the HTML is big or very complex.

HTML2PS: same as DOMPDF, but this one converts first in .ps (GhostScript), then, in whatever format you need (PDF, JPEG, PNG). For me it is a little better then DOMPDF, but have the same speed problem. Oh, better compatibility with CSS.

Those two are PHP classes, but if you can install some software on the server, and access it through passthru() or system(), give a look to these too:

wkhtmltopdf: based on WebKit (Safari's wrapper), it's really fast and powerful. Seems like it is the best one (at the moment) for HTML to PDF conversion on the fly, taking only 2 seconds for a 3 page XHTML document with CSS2. It's a recent project anyway, so the google.code page is often updated.

htmldoc : this one is a tank, it really never stops/crashes. The project seems dead, as of 2007, but if you don't need CSS compatibility this can be nice for you.

tcpdf - this is an enhanced and maintained version of fpdf. It has all the main Features of tpdf and it also has faster execution time with great output. For detailed tutorial on using the two most popular PDF generation classes: TCPDF and FPDF, please follow this link. I think you should continue using TCPDF.

See these posts also:

  1. Convert HTML + CSS to PDF with PHP?
  2. Which one is the best PDF-API for PHP?
  3. Export a html into PDF in PHP?
  4. Writing HTML with PHP variables to PDF file?
  5. How to convert html into pdf with php?
  6. Tool for exporting html as pdf
  7. Converting HTML in PHP File to PDF File

Laravel PDF Exports: How to export data based by id

This code is for exportpdf() function

// This will convert url string to array with '/' declimiter
$url = explode('/', url()->current()); // something like [..., '127.0.0.1:8000', 'pengajuan', '3']
$id = end($url); // result is 3

// Get Specific Submission detail with "where" function
$submissionDetail = SubmissionDetail::where('submission_id', $id)->first();

// Rest is just same
$pdf = PDF::loadview('pengajuan_detail', ['submissionDetail' => $submissionDetail]);
return $pdf->stream();

If error occurs, it can be from your PDF template (you can post error if it happens) since you directly using PDF file as a template, not the blade file

Export a php page to pdf

for using FPDF you can download from the link and for more information , Here's http://fpdf.org

after that you can integrate as follows..

<?php

require_once('fpdf.php');

class PDF extends FPDF
{
function Header()
{
$query='your query';
$row=mysql_fetch_assoc($query); // data from database

$this->SetXY(50,60);
$this->Cell(45,6,'Name :',1,1,'R');
$this->SetXY(95,60);
$this->Cell(80,6,$row['pdf_name'],1,1);

$this->SetXY(50,66);
$this->Cell(45,6,'Email Address :',1,1,'R');
$this->SetXY(95,66);
$this->Cell(80,6,$row['pdf_email'],1,1);

$this->SetXY(50,72);
$this->Cell(45,6,'Question Paper Selected :',1,1,'R');
$this->SetXY(95,72);
$this->Cell(80,6,$row['subject_sel'],1,1);

$this->SetXY(50,78);
$this->Cell(45,6,'Total Correct Answers :',1,1,'R');
$this->SetXY(95,78);
$this->Cell(80,6,$row['right_answered'],1,1);

$this->SetXY(50,84);
$this->Cell(45,6,'Percentage :',1,1,'R');
$this->SetXY(95,84);
$this->Cell(80,6,$row['percentage']."%",1,1);

$this->Ln(20);
}
function Footer()
{
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->Cell(0,10,'abc according to you',0,0,'C');
}
}
$pdf=new PDF('P','mm',array(297,210));
$pdf->Output($name.'.pdf','D');
?>

you can edit according to your need, it will display record in the form of table and generate it to pdf. hope it will helpful to you.

update:

for image

$this->Image('image path',80,30,50);

How can I export my php page to PDF using php?

You can try mpdf class.

Just download mpdf class and use the following script to print your php page. Save the below code as mypdfgenerator.php.:

<?php 
include("mpdf.php");
$mpdf=new mPDF('win-1252','A4','','',15,10,16,10,10,10);//A4 page in portrait for landscape add -L.
$mpdf->SetHeader('|Your Header here|');
$mpdf->setFooter('{PAGENO}');// Giving page number to your footer.
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetDisplayMode('fullpage');
// Buffer the following html with PHP so we can store it to a variable later
ob_start();
?>
<?php include "phppage.php";
//This is your php page ?>
<?php
$html = ob_get_contents();
ob_end_clean();
// send the captured HTML from the output buffer to the mPDF class for processing
$mpdf->WriteHTML($html);
//$mpdf->SetProtection(array(), 'user', 'password'); uncomment to protect your pdf page with password.
$mpdf->Output();
exit;
?>

How to create a PDF file with PHP?

You can try free libraries like fPdf

FPDF is a PHP class which allows to
generate PDF files with pure PHP, that
is to say without using the PDFlib
library. F from FPDF stands for Free:
you may use it for any kind of usage
and modify it to suit your needs.



Related Topics



Leave a reply



Submit