I'm trying to save HTML content generated by PHP as a PDF file.
To do this I found FPDF.
My script is as follows:
if(isset($_POST['content_to_save']) && isset($_POST['name_to_save'])){
$file_name = $_POST['name_to_save'];
$file_content = $_POST['content_to_save'];
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,$file_content);
$content = $pdf->Output('../my_folder/'.$file_name.'.pdf','F');
}
My two variables ($file_name & $file_content) are set as I want them with no issues and it creates the PDF in the correct location with the correct file name, however the actual content is the HTML in text format rather than the actual rendered HTML.
Edit
I have now started trying to use TCPDF
My code now is as follows:
if(isset($_POST['po_content_to_save']) && isset($_POST['po_name_to_save'])){
$file_name = $_POST['po_name_to_save'];
$file_content = $_POST['po_content_to_save'];
require('TCPDF/tcpdf.php');
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->writeHTML($file_content);
$pdf->Output('../my_folder/'.$file_name.'.pdf', 'I');
}
However, now I get the following error:
TCPDF ERROR: Some data has already been output, can't send PDF file
So after alot of trial and error with a few different libraries I finally found Dompdf
The following code now does what I was trying to achieve:
require '../vendor/autoload.php';
use Dompdf\Dompdf;
if(isset($_POST['po_content_to_save']) && isset($_POST['po_name_to_save'])){
$file_name = $_POST['po_name_to_save'];
$file_content = $_POST['po_content_to_save'];
$document = new Dompdf();
$document->loadHtml($file_content);
$document->setPaper('A4', 'portrait');
$document->render();
$output = $document->output();
file_put_contents('../my_folder/'.$file_name.'.pdf', $output);
}
Related
I am using TCPDF to generate the PDF.
I have two files.The contents of the first file are the invoice contents (table, photo, etc..../without code TCPDF)
The contents of the second file are the following code(Coded by my colleague).
$_factorUrl = PathAllocator::getBaseUrlByPath(__FILE__)."modules/printer/views_pdf/".$billName.".php?order_id=".$orderId;
ob_start();
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//Before Write
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
//After Write
$pdf->setRTL(true);
// set font
$pdf->SetFont('dejavusans', '', 12);
// add a page
$pdf->AddPage();
// Persian content
$pdf->WriteHTML(execute($_factorUrl), true, 0, true, 0);
//Close and output PDF document
//$pdf->Output("factor.pdf");
ob_end_clean();
$pdfFileName = $billName."-".$orderId.md5(rand(1,1000).microtime().UserAuth::getCustomerId().$billName.$orderId);
$_fullFilePath = $sourcePath."modules/printer/dl/".$pdfFileName.".pdf";
$pdf->Output($_fullFilePath, 'FI');
header("location: ".PathAllocator::getBaseUrlByPath(__FILE__)."modules/printer/dl/".$pdfFileName.".pdf");
Now I have the output(second image), But no style is applied, the tables do not look good and ...
when I add any tcpdf statement to first file, I have no output.
for example I add to the first file :
<?php
$html = '<h1>Example of HTML text flow</h1>';
?>
first image is Invoice appearance on web page
second image is Invoice pdf
you have to use tcpdf image for background image
https://tcpdf.org/examples/example_009/
and you need to use tcpdf table
https://tcpdf.org/examples/example_045/
Now I'm doing a task about exporting data into PDF from CodeIgniter framework. I'm using FPDF http://www.fpdf.org/ for exporting the PDF. And here is my controller:
public function exportPDF()
{
$pdf = new FPDF('l','mm','A4');
// create new page
$pdf->AddPage();
// setting font type
$pdf->SetFont('Arial','B',8);
$pdf->Cell(40,6,'オーダー番号',1,0);
$pdf->Cell(35,6,'デリバリー日',1,0);
$pdf->Cell(30,6,'カスタマー名',1,0);
$pdf->Cell(60,6,'住所',1,0);
$pdf->Cell(12,6,'数量',1,1);
$pdf->SetFont('Arial','',8);
//fetch data
$res['data'] = $this->session->userdata('logistic_account');
$data['orderlist'] = $this->Bll_products->exportPDF($res['data']['logistic_id']);
foreach ($data['orderlist'] as $row) {
$pdf->Cell(40,6,$row->order_number,1,0);
$pdf->Cell(35,6,$row->delivery_date,1,0);
$pdf->Cell(30,6,$row->name,1,0);
$pdf->Cell(60,6,$row->address,1,0);
$pdf->Cell(12,6,$row->order_quantity,1,1);
}
$filename = $res['data']['pic_name']."_".date("Y-m-d H:i:s").'.pdf';
$pdf->Output($filename, 'D');
}
But the result like this:
How do I can display a Japanese word in that .pdf file?
I want to use storeAs() but it doesn't work (Dompdf library)
How to put the converted PDF file in storage ?
$data = ['discription' => 'tifa'];
$pdf = PDF::loadView('file.viewFile', compact('data'))->storeAs("public/uploud",'filename');
return $pdf->stream("file.pdf");`
$data = ['discription'=>'tifa'];
$pdf =PDF::loadView('file.viewFile',compact('data'));
$content = $pdf->output();
$x= storage_path("app/public/uploud/anyname.pdf");
file_put_contents($x, $content);
return $pdf->stream(" anyname.pdf");
Hi I am trying to save dompdf to a specific directory in codeigniter file and then taking the saved file and sending it as a email. I am using codeigniter to do all this work.
Here is the function.
public function generateCreditAggreement(){
$this->load->helper('file');
require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');
$pdfroot = dirname(dirname(__FILE__));
$pdfroot .= '/third_party/pdf/';
$dompdf = new Dompdf();
$msg = $this->load->view('credit_agreement_view', '', true);
$html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
$dompdf->load_html($html);
$paper_orientation = 'Potrait';
$dompdf->set_paper($paper_orientation);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Download folder
// $dompdf->stream('document.pdf');
//save the pdf file on the server
$pdf_string = $dompdf->output();
file_put_contents($pdfroot, $pdf_string );
}
The error I am getting is that
Message: file_put_contents(C:\Apache24\htdocs\faceloan\faceloan\application/third_party/pdf/): failed to open stream: No such file or directory
But the directory does exist , and the path name is right.
The path .../third_party/pdf/ is probably a directory and not a file. PHP can't save your data as a directory. Specify a filename as first parameter of file_put_contents(), e.g. third_party/pdf/my_document.pdf.
I'm using TCPDF to print a receipt and then send it to customer with phpMailer, but I have a problem:
I have no idea how to save the file into a pdf.
I've tried this:
// reset pointer to the last page
$pdf->lastPage();
//Close and output PDF document
$pdf->Output('kuitti'.$ordernumber.'.pdf', 'I');
$this->Output("kuitit");
try this
$pdf->Output('kuitti'.$ordernumber.'.pdf', 'F');
this stores the generated pdf file in your custom folder of your project
$filename= "{$membership->id}.pdf";
$filelocation = "D:\\wamp\\www\\project\\custom";//windows
$filelocation = "/var/www/project/custom"; //Linux
$fileNL = $filelocation."\\".$filename;//Windows
$fileNL = $filelocation."/".$filename; //Linux
$this->pdf->Output($fileNL, 'F');
$pdf->Output() takes a second parameter $dest, which accepts a single character. The default, $dest='I' opens the PDF in the browser.
Use F to save to file
$pdf->Output('/path/to/file.pdf', 'F')
Only thing that worked for me:
// save file
$pdf->Output(__DIR__ . '/example_001.pdf', 'F');
exit();
For who is having difficulties storing the file, the path has to be all the way through root. For example, mine was:
$pdf->Output('/home/username/public_html/app/admin/pdfs/filename.pdf', 'F');
TCPDF uses fopen() to save files.
Any paths passed to TCPDF's Output() function should thus be an absolute path.
If you would like to save to a relative path, use e.g. the __DIR__ global constant (see this answer).
If you still get
TCPDF ERROR: Unable to create output file: myfile.pdf
you can avoid TCPDF's file saving logic by putting PDF data to a variable and saving this string to a file:
$pdf_string = $pdf->Output('pseudo.pdf', 'S');
file_put_contents('./mydir/myfile.pdf', $pdf_string);
nick's example saves it to your localhost.
But you can also save it to your local drive.
if you use doublebackslashes:
$filename= "Invoice.pdf";
$filelocation = "C:\\invoices";
$fileNL = $filelocation."\\".$filename;
$pdf->Output($fileNL,'F');
$pdf->Output($filename,'D'); // you cannot add file location here
P.S. In Firefox (optional) Tools> Options > General tab> Download >Always ask me where to save files
$pdf->Output( "myfile.pdf", "F");
TCPDF ERROR: Unable to create output file: myfile.pdf
In the include/tcpdf_static.php file about 2435 line in the static function fopenLocal if I delete the complete 'if statement' it works fine.
public static function fopenLocal($filename, $mode) {
/*if (strpos($filename, '://') === false) {
$filename = 'file://'.$filename;
} elseif (strpos($filename, 'file://') !== 0) {
return false;
}*/
return fopen($filename, $mode);
}
require __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$html = '<h2>Welcome toTcpdf</h2>';
$html.= '<p>Welcome toTcpdf</p>';
//$pdf->writeHTML($tbl, true, false, true, false, '');
$pdf->writeHTML($html, true, false, false, false, '');
$time = time();
$sFilePath = __DIR__ . '/upload/'.$time.'.pdf' ;
$pdf->Output( $sFilePath , 'F'); // Save to folder
//$pdf->Output( $sFilePath , 'I'); // view to browser
//$pdf->Output( $sFilePath , 'D'); // Download instant
this is working
You may try;
$this->Output(/path/to/file);
So for you, it will be like;
$this->Output(/kuitit/); //or try ("/kuitit/")
This worked for me, saving to child dir(temp_pdf) under the root:
$sFilePath = $_SERVER['DOCUMENT_ROOT'] . '//temp_pdf/file.pdf' ;
$pdf->Output( $sFilePath , 'F');
Remember to make the dir writeable.