Record.php
<?php
if(isset($_POST['btn'])){
if(!isset($error)){
//create html of the data
ob_start();
?>
<?php
$body = ob_get_clean();
$body = iconv("UTF-8","UTF-8//IGNORE",$body);
include("mpdf/mpdf.php");
$mpdf=new \mPDF('c','A4','','' , 0, 0, 0, 0, 0, 0);
//write html to PDF
$mpdf->WriteHTML($body);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML(file_get_contents('Records.php'));
$mpdf->Output();
}
}
?>
I'm trying to make a pdf file of my current file. It has a table which data is fetching from database. The Problem is when i click on the button it redirects me to the pdf file but it gives me the error and the error is 'Failed to load PDF document'.You can see the error below.What am i doing wrong??
Make things simple:
<?php
include("mpdf/mpdf.php");
echo '<table border="1">';
echo '<tr><td>Put some content here!</td></tr>';
echo '</table>';
$html = ob_get_contents();
$filename = 'filename.pdf';
$mpdf = new mPDF('utf-8', 'A4-P');
$mpdf->WriteHTML($html);
$mpdf->Output($filename,'I');
Don't need to convert charset, this library is already running in UTF-8.
Related
I have a php file that downloads PDFs. I call the php as GeneratePDF.php?id=123 and the php looks like this:
<?php
use setasign\Fpdi\Fpdi;
session_start();
ob_start();
require_once __DIR__ . '/vendor/autoload.php';
require_once('FPDI/autoload.php');
require_once('fpdf/fpdf.php');
require_once('FPDI/Fpdi.php');
$pdf = new \Mpdf\Mpdf();
$pdf->AddPage();
$pdf->setSourceFile('sample.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 0, 0, 297, 420, true);
$pdf->AddPage();
$tplIdx2 = $pdf->importPage(2);
$pdf->useTemplate($tplIdx2, 0, 0, 297, 420, true);
$id=$_GET['id'];
$pdf->Output("$id.pdf", "D");
ob_end_flush();
?>
I am now using another php file to download a set of GeneratePDF.php such as
GeneratePDF.php?id=123
GeneratePDF.php?id=456
GeneratePDF.php?id=789
From this php, I want to zip these 3 files and download it. I know the way to do it would be to use ZipArchive but how do I do I call all 3 GeneratePDF.php and add it to zip?
ZipArchive.php
<?php
session_start();
ob_start();
$zip = new ZipArchive;
$tmp_file = "PDF_Documents_".date('Y_m_d').".zip";
//How do I add set of pdfs from GeneratePDF.php?
} else {
echo 'Failed!';
}
Can someone help me figure this out?
I have a php script where I can easily download a pdf file using fpdf library. The script takes in ID of multiples rows from DB and downloads PDFs for each ID. How do I download multiple pdfs as once with zip? That is I send ID value to another script and that script then collects all IDs and downloads it in zip file?
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
$db = new PDO("mysql:host=localhost; dbname=db;charset=utf8",'root','');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
//$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(5);
}
function viewTable($db)
{
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
}
}
$id=$_GET['id'];
$pdf = new PDF('P','mm','A4');
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->viewTable($db);
$pdf->Output("D", "$id.pdf");
ob_end_flush();
?>
Got it! I created ZipArchive and sent file path to zip file.
$pdf->Output("F", "$id.pdf");
$zip = new ZipArchive;
$tmp_file = 'Documents.zip';
//if ($zip->open($tmp_file, ZipArchive::CREATE))
if($zip->open($tmp_file, ZipArchive::OVERWRITE|ZipArchive::CREATE)) {
$zip->addFile("$id.pdf", "$id.pdf");
$zip->close();
echo 'Archive created!';
header('Content-disposition: attachment; filename=Documents.zip');
header('Content-type: application/zip');
} else {
echo 'Failed!';
}
ob_end_clean();
readfile($tmp_file);
I am using the Mpdf in symfony. I have installed the mpdf through composer like:
composer require mpdf/mpdf
After that require the Mpdf.php in autoload.php.
Then use the code for mpdf is:
$mpdf = new mPDF();
$html = '<p style="color:red;">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
CSS is not affecting on the HTML. When I'm using the style on tag then it's working fine.
$mpdf = new mPDF();
$html = '<style>p{color:red;}</style><p>PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
When I try to use the CSS with class or ID then also not affecting.
$mpdf = new mPDF();
$html = '<style>p.text-color{color:red;}</style><p class="text-color">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
You don't need the style tag you can do it as follow:
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
So write your CSS first then your html that was working for me the last time.
https://mpdf.github.io/css-stylesheets/introduction.html
I have a situation like create multiple pdf files and download as zip file.
I have tried with below code
function downloadZip()
{
if ($this->input->post('btn_submit'))
{
$data = array(
'results' => $this->Mdi_download_invoices->download_pdf_files($this->input->post('client_id'))
);
foreach ($data['results'] as $d)
{
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('download_all_invoices/pdf',$d,true);
$mpdf->WriteHTML($html);
$mpdf->Output(); //this will create a pdf file in next tab
$this->load->library('zip');
$this->zip->add_data();
$this->zip->archive('/var/www/my_backup.zip');
$this->zip->download('my_backup.zip');
}
}
How can I store all pdf file in the array??
Try this code.
$i = 0;
foreach ($data['results'] as $d)
{
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('download_all_invoices/pdf',$d,true);
$mpdf->WriteHTML($html);
$mpdf->Output(); //this will create a pdf file in next tab
$this->zip->add_data('file-'.$i.'.pdf',$d);
$i++;
}
$this->zip->archive('/var/www/my_backup.zip');
$this->zip->download('my_backup.zip');
By this you can add each PDF file in Zip and after you the loop you can generate a zip file and download it. I have not tested it so please ignore syntax error but you will get an idea from it.
I'm looking to start with an initial PDF file, one that has graphics and text, and then take some html code which has dynamic values for some user input, generate that as a PDF, hopefully either using the initial PDF as a background, OR somehow running a PHP script afterwards to "merge" both PDF where one acts as a background to another.
I have some code that renders an HTML formatted PDF: (using DOMPDF)
$initialpdf = file_get_contents('file_html.html');
$initialpdf = str_replace(array(
'%replaceText1%',
'%replaceText2%'
), array (
$replaceText1,
$replaceText2,
), $initialpdf);
$fp = fopen('file_html_new.html','w');
file_put_contents('file_html_new.html', $initialpdf);
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper,$orientation);
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
#file_put_contents($filename . ".pdf", $pdf);
}
$filename = 'HTML_Generated_pdf';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html_new.html');
pdf_create($html,$filename,'Letter','landscape');
The code above takes html file "file_html.html" and does string replacements with user input values, renders this as a new HTML file called "file_html_new.html" and then renders that AS a PDF.
I also have other PHP code that render a PDF by having a PDF as an initial source: (using FPDF)
<?php
ob_clean();
ini_set("session.auto_start", 0);
define('FPDF_FONTPATH','font/');
define('FPDI_FONTPATH','font/');
require('fpdf.php');
require('fpdi.php');
$pdf = new FPDI();
$pdf->setSourceFile("/home/user/public_html/wp-content/myPDF.pdf");
$tplIdx = $pdf->importPage(1);
$specs = $pdf->getTemplateSize($tplIdx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L', 'Letter');
$pdf->useTemplate($tplIdx, 0, 0);
$pdf->SetFont('helvetica');
$pdf->SetXY(30, 30);
$pdf->Write(0, $replaceText1);
ob_end_clean();
$pdf->Output('New_Generated_PDF.pdf', 'F');
?>
This takes an already existing PDF, "myPDF.pdf", and uses it as a background, writing some passed in value to the document, and saving the newly produced document.
While this is essentially what I want to do, I need to work with html because the exact formatting for text gets rigorous and almost impossible to do just by plotting it in manually.
I'm open to using DOMPDF, FPDF, FPDI, TCPDF, or any other PHP resource in order to accomplish this.
Is there a way to fuse the two ways I have above?
For sure you can use different existing PDF documents with FPDI, too. This code should show you the concept (actually I guess that all page formats are A4 portrait):
<?php
$pdf = new FPDI();
// let's get an id for the background template
$pdf->setSourceFile('myPDF.pdf');
$backId = $pdf->importPage(1);
// iterate over all pages of HTML_Generated_pdf.pdf and import them
$pageCount = $pdf->setSourceFile('HTML_Generated_pdf.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// add a page
$pdf->AddPage();
// add the background
$pdf->useTemplate($backId);
// import the content page
$pageId = $pdf->importPage($pageNo);
// add it
$pdf->useTemplate($pageId);
}
$pdf->Output();