Generating PDFs with PHP

Create PDF file using PHP

I have used TCPDF with much success to generate PDF files programatically using PHP.

And to generate word documents: http://www.phpdocx.com/ (this is a paid solution).

best plugin to generate pdf from php

I am not sure which one that fits your requirements.
But I seen much people talking good about tcpdf
See the list below of a couple:

  • ApacheFOP
  • dompdf
  • FPDF
  • html2ps
  • mPDF
  • PDFlib
  • TCPDF
  • wkhtmltopdf
  • Zend_Pdf

source:
Which one is the best PDF-API for PHP?

How to create pdf using php

Generate the PHP as you usually would but add the header

header("Content-Type: application/pdf");
header('Content-Disposition:inline; filename="testing.pdf"'); //view in browser

Or

 header("Content-Type: application/pdf");
header('Content-Disposition:attachment; filename="testing.pdf"'); //force download

How to generate PDF from an html page

Finally I found answer for my question. First thing to do is download the pdf folder that you are using. Here I use MPDF.
Here my html code for button:

<button class='pdfupload' id='".$userid."' dataoffset='".$group_id."'>Download</button>

From $userid and $group_id we will get the displayed form datas. In button click an ajax call is triggered.

jQuery(document).ready(function(){
jQuery('.pdfupload').click(function(){
var group_id = jQuery(this).attr('dataoffset');
var ID = jQuery(this).attr('id');
jQuery.ajax({
type:'POST',
url:"<?=site_url()?>/uploadpdf.php",
data:{group_id:group_id, ID:ID},
success:function(result){
window.location.replace('<?=site_url()?>/filegen.php?name='+result);
}
});
});
});

In ajax page the pdf will be generated. To generate pdf file mpdf.php need to be included. Below given is the code:

include('MPDF57/mpdf.php');
include 'wp-load.php';
global $wpdb;
$date = date("Ymdhis");
$filename = $id.'trialbasic'.$date.'.pdf';
$path = dirname(__FILE__).'/pdfs/'.$filename;
$html = "Your pdf content"; //Here in my code the details using `$userid` and `$group_id` will be fetched
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output($path,'F');
echo $filename;

Here PDF file will be generate and stored in $path. The return of ajax will be the name of the PDF file. In the success part filegen.php is called in order to directly download the PDF file on click. The result of ajax will be passed to filegen.php The code in filegen.php is given below:

include 'wp-load.php';
$name = $_GET['name'];
$path = dirname(__FILE__).'/pdfs/'.$name;
header('Content-Disposition: attachment; filename=' . $name); // Make the browser display the Save As dialog
readfile($path); //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb

Finally thanks all for sparing your time to help me.

php-PDF file not generating(using FPDF)

You should add file name;

$pdf->Output("myPdfFile.pdf", "F");

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