How to export PHP/MYSQL data to PDF? - php

I have created a Student Information Form and it is
connected to a MYSQL database and I want to export all the submitted data from MYSQL DB to PDF ?
How to do that ?

You should make use of PDF creator libraries like: FPDF , TCPDF, EzPDF
All three are easy to learn in the order I've put. The documentation of FPDF and EzPDF are very neat and clean. But TCPDF Documentation is not that readable.
Checkout this site: http://www.tcpdf.org/. It also allows to output document created on the fly easily into a browser.
Also there is a good example of doing this: here is the link: https://www.phpflow.com/php/generate-pdf-file-mysql-database-using-php/
Hope this answer is helpful to you...

<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
foreach($result as $row) {
$pdf->SetFont('Arial','',12);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(90,12,$column,1);
}
$pdf->Output();
?>

Related

Is it possible to Generate a Pdf Design by using Fpdf

I want to Generate a Pdf in Php Using FDPF. can we draw a table in fpdf. I want to Design a Table In the Image given Using FPDF.Is it Possible. I am new to fpdf Please help me. I want Above table design. Thanks in Advance
you should download an addon called PDF_MC_TABLE or copy it from here: http://www.fpdf.de/downloads/addons/3/
$pdf = new PDF_MC_Table();
$pdf->Row(array("this\nis a test","with a multi\ncell"));
$pdf->Output();
You can try this.. It is not the correct design for what you are looking for, but it will helpful to you to create a table.
require("fpdf.php");
$pdf->SetFont('Arial','B',16);
$pdf->SetTextColor(192,192,192);
$pdf->Cell(60,10,'Name:',1,0,'C',false,0);
$pdf->Cell(130,10,'Ali',1,1,'C');
$pdf->Cell(60,10,'Subject:',1,0,'C',0);
$pdf->Cell(130,10,'Maths',1,1,'C');
$pdf->Cell(60,10,'Maximum Marks:',1,0,'C',0);
$pdf->Cell(130,10,100,1,1,'C');
$pdf->Cell(60,10,'Marks Obtained:',1,0,'C',0);
$pdf->Cell(130,10,88,1,1,'C');
$pdf->Cell(60,10,'Percentage:',1,0,'C',0);
$pdf->Cell(130,10,88,1,1,'C');
$pdf->Output();

Convert HTML form data into a PDF file using PHP

I have been looking and testing this for a couple days now and was wondering if anyone could point me in a different direction. I have a very long job application HTML form (jobapp.html) and a matching PDF (jobpdf.pdf) that have the same field names for all entries in both the HTML form and the PDF. I need to take the user data that is entered in the form and convert it to a PDF. This is what I have gathered so far but don't know if I am on track:
Is pdftk the only viable 3rd party app to accomplish this?
Using pdftk would i take the $_POST data collected for the user and generate a .fdf(user.fdf) then flatten the .fdf on the .pdf(job.pdf). So irreguarless of where the fields are located on each document the information on the fdf would populate the pdf by field names?
I have been trying
http://koivi.com/fill-pdf-form-fields/tutorial.php
I have also looked at "Submit HTML form to PDF"
I have used fpdf several times to create php-based pdf documents. An example following:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddFont('georgia', '', 'georgia.php');
$pdf->AddFont('georgia', 'B', 'georgiab.php');
$pdf->AddFont('georgia', 'I', 'georgiai.php');
# Add UTF-8 support (only add a Unicode font)
$pdf->AddFont('freesans', '', 'freesans.php', true);
$pdf->SetFont('freesans', '', 12);
$pdf->SetTitle('My title');
$pdf->SetAuthor('My author');
$pdf->SetDisplayMode('fullpage', 'single');
$pdf->SetLeftMargin(20);
$pdf->SetRightMargin(20);
$pdf->AddPage();
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
You can learn very fast with these tutorials from the website itself.
EDIT: Example to save form data: (yes, is very easy...)
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
foreach ($_POST as $key =>$data)
{
$pdf->Write(5, "$key: $data"); //write
$pdf->Ln(10); // new line
}
$pdf->Output($path_to_file . 'file.txt','F'); // save to file
Look at these pages created with fpdf, really!
http://www.fpdf.org/
That would be the library to do it. I used it here to add images to a form and submit it to create a PDF with those images: http://productionlocations.com/locations
The actual code to do it is pretty complex.
I have found PrinceXML very easy to use. It takes your HTML/XML, applies CSS, and converts it into a PDF. The PHP extensions work very well. Unfortunately, it's not free.
One way you can consider is using an online API that converts any HTML to PDF. You can send them a generated HTML (easier to produce) that will contains your user's submitted data, and receive back a high fidelity PDF.
There are quite a few services available on the market. I like to mention PDFShift because it offers a package in PHP that simplifies the work for you.
Once you've installed it (using Composer, or downloaded it directly, depending on your choices) you can quickly convert an HTML document like this:
require_once('vendor/autoload.php');
use \PDFShift\PDFShift;
PDFShift::setApiKey('{your api key}');
PDFShift::convertTo('https://link/to/your/html', null, 'invoice.pdf');
And that's it. There are quite a few features you can implement (accessing secured documents, adding a watermark, and more).
Hope that helps!

Generate PDF with PHP

how generate PDF document with MYSQL query more than 15 fields with wrap text? I have generated PDF with fpdf option. but I have created only 8 fields with nowrap text. so any one please solve the solutions. thanks.
For example. I have created with nowrap text.
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(4,126,167);
$pdf->Cell(10,10,'Worksheet Report');
$pdf->Ln();
//Column titles
$header=array('Client','Team In Charge','Staff In Charge','Priority','Master Activity','Sub Activity','Last Reports Sent','Job in Hand','Team Incharge Notes','External Due Date','Befree Due Date','Status');
$pdf->SetFont('Arial','B',6);
$pdf->FancyTable($header);
$pdf->SetFont('Arial','',5);
$pdf->SetFillColor(255,255,255);
$pdf->SetTextColor(0);
$pdf->SetLineWidth(.2);
$sql=$_SESSION['query'];
$result = mysql_query($sql);
while($row = #mysql_fetch_array($result)){
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_CompanyName"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_TeamInCharge"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_StaffInCharge"]),1,0,'T',true);
$pdf->Cell(12,10,htmlspecialchars($row["lp_wrk_priority"]),1,0,'T',true);
$pdf->Cell(17,10,htmlspecialchars($row["lp_wrk_MasCode"]).($row["lp_wrk_MasCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_MasterActivity"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_SubCode"]).($row["lp_wrk_SubCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_SubActivity"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_Details"]),1,0,'T',true);
$pdf->Cell(18,10,htmlspecialchars($row["wrk_Notes"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_TeamInChargeNotes"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_DueDate"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_InternalDueDate"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_Status"]),1,0,'T',true);
$pdf->Ln();
}
There's a webservice called DocRaptor that uses Prince to generate pdf and xls from html. They offer a free plan as well as larger paid plans.
check this web link :-
http://qualitypoint.blogspot.com/2010/07/resolved-wrap-text-issue-in-fpdf-table.html
http://blog.themeforest.net/tutorials/how-to-create-pdf-files-with-php/
I hope it is useful for you :)
Have a look at the dompdf class. It generates the pdf from your html code so it's much easier and quicker to get the results you want. It's still a bit finicky with floats so you have to imagine your are in the 90's and do a table based design.
My advice is to print out the table in html, than convert in into pdf: is easier, faster, and more flexible: when you have the html documents, will be easy (for example) convert into a jpg, if you'll need to in the future.
If you can install software on your server, i raccomend you wkhtmltopdf.
You generate the html file, and wkhtmltopdf convert it to pdf, with full (webkit based) css support.
Else, i've found that html2ps doeas a great job, but is quite slower with large files.. css support is good, but no guarantee that it will support css3, for example.

Generate PDF report from php

I am using php code to query to a database and the results will be used to generate a report.
If I want the report to be generated in a pdf format how should I do it ?
If you need UTF support in your PDF file, consider tcpdf library.
Download it from here: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
And in your script:
<?php
//include files
require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//add some content using class methods
//Close and output PDF document
$pdf->Output('filename.pdf', 'I');
?>
Look into html2pdf
Create your report as html and then run the code to transform into PDF. You don't need to know the language to generate the PDF blocks. Submitted forms work out cool too.
You can also use FPDF, I've used that for several projects. In the beginning you will be annoyed a lot, but when you get used to it, it will get easier to create pdf's :)
I've used TCPDF (http://www.tcpdf.org/) for my last project. It worked pretty good but the next time im going for a html to pdf converter, simply because designing the report (converting to pdf draw statements) was such a time sink.
So i would suggest http://sourceforge.net/projects/html2fpdf/

Edit PDF files programmatically in PHP

Background info:
I got a project to produce a customised PDF on the fly from a given PDF file using PHP. All I need it to do is to replace strings, e.g. search in "template.pdf" for "{Address}", replace with "Street Name".
I've seen links to fpdf/pdfi/dompdf etc., but can't find any useful example code that I could use :s.
Any help / pointers would be greatly appreciated.
fpdf is fantastic, you need to use somthing else to import an exisitng PDF though, See below.
http://www.setasign.de/products/pdf-php-solutions/fpdi/
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pagecount = $pdf->setSourceFile('TestDoc.pdf');
$tplidx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);
$pdf->Output('newpdf.pdf', 'D');
decided to generate html web page (PHP) then use wkhtmltopdf
(http://code.google.com/p/wkhtmltopdf)
to produce the pdf bit of a work around but less hastle
PDFlib (with the additional PDI) from pdflib.com should be able to do this for you. Admittedly it is pretty pricey, so there may be other options, too :)

Categories