Zip FPDFs files? - php

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);

Related

html2pdf zip multiple pdf files php mysql

I need to zip multiple pdf documents generated with html2pdf. I managed to create the pdf files in the tmp/ directory but the zip file created cannot be opened. Any idea ?
This is what I did:
<?php
$tmp='/opt/lampp/htdocs/rapport/tmp';
$file = '/opt/lampp/htdocs/rapport/tmp/my-pdf.zip';
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
ob_start();
Here the part to save the zip file with all the pdf files inside:
try
{
$html2pdf = new Html2Pdf('P', 'A4', 'fr');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
//$content=ob_end_clean();
//$html2pdf->Output($tablename.'-'.$idEcht.'-NGSReport.pdf','F');
//$html2pdf->Output(__DIR__ .$tablename.'-'.$idEcht.'-NGSReport.pdf','F');
$html2pdf->Output('/opt/lampp/htdocs/rapport/tmp/'.$tablename.'-'.$idEcht.'-NGSReport.pdf','F');
// ADD PDF FILE TO ZIP
$zip->addFile('/opt/lampp/htdocs/rapport/tmp/'.$tablename.'-'.$idEcht.'-NGSReport.pdf');
unset($html2pdf);
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
}
}
//After I loop ends, I close the zip file and force download
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="my-pdf.zip"');
readfile('my-pdf.zip');
mysqli_close($conn);

Zip and download multiple PDFs from php

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?

Failed to load PDF document?

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.

exporting database into pdf file in php

<?php
mysql_connect('localhost','root','');
mysql_select_db('mydatabase');
ob_start();
header('Content-Type: application/pdf');
include_once('pdf/fpdf.php');
$pdf=new FPDF();
get_class_methods($pdf);
$pdf->AddCol('firstname',30,'firstname');
$pdf->AddCol('lastname',30,'lastname');
$pdf->AddCol('address1',30,'Address');
$pdf-> AddCol('address2',30,'Address');
$pdf->AddCol('postCode',30,'Post Code');
$pdf->AddCol('towns',30,'Town');
$pdf->Table("select firstname,lastname from mytable");
$pdf->Output();
ob_end_flush();
This is my PHP code for exporting a database into a PDF document. It is showing an error as failed to open the PDF document file.
Try below code :-
<?php
require_once('fpdf17/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Write(5,"Hello, World!\n\n\n");
$pdf->Output('pdf.pdf', 'F');
?>

Zend PDF Tables Helper

Is there any Zend Helper that can make PDF document with tables.
i need to pass result of my Query and the helper will return a PDF document with data in a table.
Just like the below Csv Helper.
what i did here is that i put the given class in
Zend/Controller/Action/Helper
and than in my Action i do so
public function getcsvAction() {
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNeverRender();
try{
$clModel = new Application_Model_DbTable_Mytable();
$select = $clModel->clCSV());
$this->_helper->Csv($select, "genertadfile name");
}catch(Exception $e){
echo 'Oops! Something went wrong.';
}
exit;
}
and this the class
<?php
// Zend_Controller_Action_Helper_Csv
class Zend_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract{
public function direct($aryData = array(), $strName = "csv", $bolCols = true){
$this->printExcel($aryData, $strName, $bolCols);
}
public function printExcel($aryData = array(), $strName = "csv", $bolCols = true){
if (!is_array($aryData) || empty($aryData)){ exit(1); }
// header
header('Content-Description: File Transfer');
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment; filename=" . $strName . "-export.csv");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-control: private, must-revalidate');
header("Pragma: public");
if ($bolCols){
$aryCols = array_keys($aryData[0]);
array_unshift($aryData, $aryCols);
}
ob_start();
$fp = fopen("php://output", "w");
if (is_resource($fp)){
foreach ($aryData as $aryLine) {
fputcsv($fp, $aryLine, ',', '"');
}
$strContent = ob_get_clean();
$strContent = preg_replace('/^ID/', 'id', $strContent);
$strContent = utf8_decode($strContent);
$intLength = mb_strlen($strContent, 'utf-8');
header('Content-Length: ' . $intLength);
echo $strContent;
exit(0);
}
ob_end_clean();
exit(1);
}
}
?>
There is a official module for pdf on zend framework 2: zendpdf
You can install it by adding zendframework/zendpdf to your composer.json file.
Its not finnished yet, that why you can't find any information about it. You can find here the previous documentation... just look for zend.pdf.*.rst files.
There is a ZF2 module for the DOMPDF library too. Find more info here. The code you can finde here. Easy installation by adding "dino/dompdf-module": "dev-master" to your composer.json file.
ADDING:
DOMPDF can be used like this:
public function dompdfAction()
{
return new DOMPDFModule\View\Model\PdfModel\PdfModel(
array(), // Variable assignments per Zend\View\Model\ViewModel
array(
'fileName' => 'monthly-report', // Optional; triggers PDF download, automatically appends ".pdf"
'paperSize' => 'a4',
'paperOrientation' => 'landscape'
)
);
}
It will generate an pdf file for the dompdfAction's viewscript.
ZendPdf can be used like this:
public function pdfAction()
{
$pdf = new \ZendPdf\PdfDocument();
$pdf->pages[] = ($page = $pdf->newPage(\ZendPdf\Page::SIZE_A4));
$font = \ZendPdf\Font::fontWithName(\ZendPdf\Font::FONT_HELVETICA);
$page->setFont($font, 36);
$page->drawText('<h1>html não funciona...</h1>', 10, 536, 'utf-8');
$page->drawText('PDF FUNCIONA', 10, 500, 'utf-8');
$image = \ZendPdf\Image::imageWithPath('public/images/zf2-logo.png');
$page->drawImage($image, 100, 100, 400, 300);
$pdf->save("teste.pdf");
}

Categories