How do I create a PDF in Magento and write something on it? Here is the sample piece that I have been playing with "on a blank page".
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
Mage::app();
/* --------------------------------------------------------- */
$pdf = new Zend_Pdf('w2.pdf');
$pdf->drawText('sample text', 420,628);
// instruct browser to download the PDF
header("Content-Type: application/x-pdf");
header("Content-Disposition: attachment; filename=w2.pdf");
header("Cache-Control: no-cache, must-revalidate");
// output the PDF
echo $pdf->save('w3.pdf');
But this is the error I'm receiving.
Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'File is not a PDF.' in /Volumes/www/vhosts/magento/lib/Zend/Pdf/Parser.php:396 Stack trace: #0 /Volumes/www/vhosts/magento/lib/Zend/Pdf.php(309): Zend_Pdf_Parser->__construct('w2.pdf', Object(Zend_Pdf_ElementFactory_Proxy), false) #1 /Volumes/www/vhosts/magento/scripts/pdf_gen.php(10): Zend_Pdf->__construct('w2.pdf') #2 {main} thrown in /Volumes/www/vhosts/magento/lib/Zend/Pdf/Parser.php on line 396
The Error doesn't make sense because the w2.pdf is an actual pdf located in the same directory. The w3.pdf file doesnt exist, would this be the problem? Also i changed
$pdf = new Zend_Pdf('w2.pdf');
to
$pdf = new Zend_Pdf();
and received this error
Fatal error: Call to undefined method Zend_Pdf::drawText()
I also tried renaming the w2.pdf to another pdf that i downloaded from the internet including the $pdf->save('file_i_downloaded) and i still get the File is not a PDF error.
This is the PDF I am using: http://www.getharvest.com/resources/invoice-template-pdf
This service here allows you to easily edit your PDF documents.
http://goo.gl/MEA3jT
You can fill out PDF form, save it, fax it, and email it.
Related
I want to export data tables to PDF.
I'm using Codeigniter Framework, and use dompdf plugin.
I think the code is correct, because it doesn't show any error information when I click the button to export to PDF, the page spends a long time loading, and in the end, it just displays "can't reach the page".
This problem also happens when I try to download files from my local directory.
Here is the code:
actually issue happened when i try to download more number of records
The controller:
$this->load->library('pdf'); // change to pdf_ssl for ssl
$data = array();
$data['result'] = $finaldata;
$data['search_header'] = $search_header;
$html = $this->load->view('admin/report/school_pdf', $data, TRUE);
$this->pdf->create($html, $filename);
$pdf = App::make('snappy.pdf.wrapper');
$rendered= view('msp.client.campaign-pdf', compact('client', 'batches', 'batch',
'openDevice','sumOfClicked'))->render();
header('Content-Type: application/pdf');
$pdf->setOption('javascript-delay', 800);
echo $pdf->getOutputFromHtml($rendered);
Here is My main controller code where can I create pdf and pass it to view file then echo the output.
I'm creating pdf file using DOM pdf, it is working but when i try to recreate it with same name and the pdf file is open then i'm getting this error message:
message: exception: "ErrorException" file:
"C:\xampp\htdocs\xxxxxxx\vendor\league\flysystem\src\Adapter\Local.php"
line:
199"file_put_contents(C:\xampp\htdocs\XXXX\storage\app\public/pdf/salary/40_file-2021.pdf):
failed to open stream: Resource temporarily unavailable" trace:
[{function: "handleError", class:
"Illuminate\Foundation\Bootstrap\HandleExceptions", type: "->"},…]
$dompdf = new Dompdf();
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$html = view('admin/pdf',['data'=>$data_new])->render();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$fileName = 'pdf/salary/'.$data_new->user_id.'_'.$data_new->application_id.'.pdf';
Storage::put('public/'.$fileName, $dompdf->output());
Any suggestion is highly appreciated.
You need to either:
Close the open PDF before attempting to overwrite it
Write the new PDF to a different location (and probably delete the old PDF after closing it)
I everybody
i dont have any problem to create .docx fil with PHPWORD
now i want to convert the .docx file created in a .pdf file
im try this simple script but not working on my configuration:
\PhpOffice\PhpWord\Settings::setPdfRendererPath('/PDF/tcpdf.php');
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
//Open template and save it as docx
$document = $phpWord->loadTemplate('edited8.docx');
$document->saveAs('temp.docx');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.docx');
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
i receive this error
An uncaught Exception was encountered
Type: PhpOffice\PhpWord\Exception\Exception
Message: PDF rendering library or library path has not been defined.
Filename: /application/php_word/vendor/phpoffice/phpword/src/PhpWord/Writer/PDF.php
Line Number: 50
anyone can help me ?
thanks a lot
You need to set Of PDF renderer.
Are you using CodeIgniter? is my code in CodeIgniter:
$rendererName = Settings::PDF_RENDERER_DOMPDF;
$rendererLibraryPath = APPPATH.('vendor/dompdf/dompdf');
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
I'm using DomPDF .. Just Custom a $rendererName and $rendererLibraryPath.
Maybe it's right...
Im trying to generate pdf using dompdf, how can I open the pdf in a new tab in a browser? Like, I Click A link for the PDF and it should open in a new tab, not save it automatically. I want to give the user a choice to save the file after seeing it first. how do i do that?
whenever i use $pdf->output at the end of the file, it does not change a thing, the file is still downloaded automatically.
please help. thanks.
Whether a PDF is downloaded or viewed in the browser depends on a couple of factors. One is your browser settings, but we have no control there. The second is how dompdf present the PDF to the browser. You can tell dompdf to offer the PDF for direct viewing using $dompdf->stream('my.pdf',array('Attachment'=>0));.
As far as opening in a new tab. That depends on how you are generating the PDF. But the simplest way it to provide a link with a target attribute.
I have a same problem into my site (http://www.pdfwebcreator.com)
My solution is:
$myfile = fopen($strFileName, "r") or die("Unable to open file!");
$fileSize = filesize($strFileName);
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"temporaryPdf.pdf\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fileSize);
echo fread($myfile, $fileSize);
}
I don't know if you got, but if you use false in this line:
$dompdf-> stream("pasta/doc/relatorio.pdf", array("Attachment" => false));
You can see the pdf in the browser.
Well that you can do with the ->stream(); at the end of the chain.
Example:
//Routes (web.php in case laravel version >= 5.4)
Route::get('/pdf', 'PdfController#pdfStream')->name('pdfStream');
//PdfController.php
public function pdfStream(Request $request) {
$data["info"] = "I is usefull!";
$pdf = PDF::loadView('whateveryourviewname', $data);
return $pdf->stream('whateveryourviewname.pdf');
}
//yourViewPage.blade.php
<a href="{{route("pdfStream")}}" target="_blank" > click me to pdf </a>
Here more information
Am still experimenting, but something like this works fine as well
$pdf->loadView('file/path', compact('values'));
return $pdf->stream();
With this you can add dynamic values to your pdf file page within the browser.