Generating PDF Symfony2 IoTcpdfBundle - pdf

I'm generating pdf file with IoTcpdfBundle using Symfony2, but there's a strange behaviour that I don't understand.
When I'm on the controller, I generate the pdf file like this:
$html = $this->renderView('MyBundle:Docs:solicituddevacaciones.pdf.twig', array());
return $this->get('io_tcpdf')->quick_pdf($html);
Those lines generate the pdf file. Everything's fine, I can right click on the file to save it and it's a .pdf file.
But when I receive some data using a form, and I put the lines inside the:
if ($request->getMethod() == 'POST') {
$year = $this->get('request')->request->get('year');
$date= $this->get('request')->request->get('date');
$html = $this->renderView('SoflaSoflaBundle:Documentales:solicituddevacaciones.pdf.twig', array());
return $this->get('io_tcpdf')->quick_pdf($html);
}
When I right click on the file to save it, it's not a .pdf file, the browser suggests that I should save the file as a .htm
Why is this happening? I need users to be able to save the files as .pdf files.
Need help with this please.

I had some similar problems and found out that the bundle might be outdated by now. I had to change some of the settings, most importantly adding another header to quick_pdf function of Tcpdf class in 'Helper' folder of the bundle:
$response->headers->set('Content-Disposition', 'attachment; filename="'.$file.'"');
Now it's working like a charm, calling it in the controller like:
$html = $this->renderView('Bundle:Item:print.pdf.twig', array(
'some' => $this->value,
));
return $this->get('io_tcpdf')->quick_pdf($html, "yourFileName.pdf");

Try to save the html (or better yet, left click instead of right click) and check the contents of that file. You probably have an error in ther somewhere, and that html file is the one generated by symfony to explain the error.

Related

WHMCS how to find right tpl files in pages

i'm using WHMCS and i want to make some changes in part of the page but its so hard and takes so much time for me to find the right tpl file and edit it in the template.
for example for this "search for domains" tab in this url =>
example.com/cart.php?a=add&domain=register
where is the .tpl file.
For example.com/cart.php?a=add&domain=register the file is at templates/orderforms/standard_cart/adddomain.tpl
In general, when url has cart.php, the file in cart template.
Also, you can add the following to hooks to tell you which tpl file was used for current page.
1- Create file includes/hooks/tpl_locator.php
2- Add the following code:
<?php
add_hook('ClientAreaPage', 1, function($vars) {
error_log(print_r([$vars['currentpagelinkback'],$vars['templatefile']], true), 3, __DIR__.'/tpl_file_location.log');
});
3- Visit the page you want to know which tpl file is used.
4- Result will be at file includes/hooks/tpl_file_location.log, sample below:
Array
(
[0] => /cart.php?a=add&pid=1&
[1] => configureproductdomain
)
This was tested on WHMCS 8.0
The tpl for cart.php?a=add&domain=register is actually located at /whmcs/templates/orderforms/standard_cart/domainregister.tpl if I understand your question.

PHPExcel write html file into existing xlsx file

I have a template file that I fill using PHPExcel. But I have terms and conditions that are saved in database with html tags and inline css. Now these terms and conditions are subject to change so I cant put it into template. So only solution is t take it from database and put it inside created template but I have no clue how to open xlsx file and insert .html file inside it perhaps as second sheet.
This is my current code:
$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($outputFileName);
And of course there is lot of code that specifically deal with writing data to excel file but that is working perfectly.
Could someone please explain how could I go about doing it.
Thanks
You can't simply insert an HTML file inside an xlsx file
The latest develop branch of PHPExcel does include an HTML to Rich Text wizard that will take a block of HTML markup and convert it to a Rich Text object that can then be stored in a cell, and /Examples/42richText.php demonstrates how it can be used. At present, this only covers basic markup tags (<br />, <font>, <b>, <i>, <em>, <strong>, <sub>, <sup>, <ins>, <del>, etc) and doesn't handle inline style in any way. However, it might provide the basis for what you want with some additional work.

Joomla remove scripts and css loaded by extensions

Using Joomla 2.5 and I have a dual instance of jquery running on one of my pages that I would like to get rid of. I tried using,
$document->getHeadData();
to no avail. The array does not contain the js files I need to unset. So what is my best option to find the js file and unload it? It seems to be getting loaded later in the page rendering process and then repopulating the head data. I am using a yoo theme template with some other extensions loaded.
I would like to avoid hard coding the template/extension files if possible since that would unload it on every page and I only want to unload it for one page.
Try this,
<?php
//get the array containing all the script declarations
$document = JFactory::getDocument();
$headData = $document->getHeadData();
$scripts = $headData['scripts'];
unset($scripts['/media/system/js/mootools-core.js']);
unset($scripts['/media/system/js/mootools-more.js']);
$headData['scripts'] = $scripts;
$document->setHeadData($headData);
?>
Or
<?php unset($this->_scripts['/media/system/js/mootools-core.js']); ?>
then simply use $document->addScript() for adding new js files.
hope its works..

phpexcel pdf rendering library has not been defined

After trying and failing to generate PDFs with PHPExcel 1.7.6 (out of memory errors), I upgraded to 1.7.8. I can't for the life of me figure out how to get it working. I've tried tcPDF and mPDF, and it's the same for both.
Putting it back to Excel output, I can see I'm setting the path correctly. All I can get is "PDF Rendering library has not been defined", and I can't figure out what it wants - I've tried 'mPDF5.4', 'MPDF54' (the actual name of the folder itself), 'mpdf', 'mpdf.php'...same each time.
I've been using PHPExcel for over a year, so I'm not entirely new to it. I've lost way more time than I care to admit on this problem, and I haven't found this problem described anywhere, so I'm feeling more than a little stupid that I appear to be the only one that can't figure this out.
The actual code I'm using is the following:
ini_set('include_path', ini_get('include_path').'\\Classes\\');
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'mPDF5.4';
$rendererLibraryPath = ini_get('include_path') . $rendererLibrary;
(That is, pretty well a copy of the example code.)
In the interest of completeness, the headers I'm using are
echo header("Content-Type: application/pdf");
echo header("Content-Disposition: attachment; filename=".$filename.".pdf" );
echo header('Cache-Control: max-age=0');
These near the top of the file, naturally.
Near the end of the file, the output code is
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
I got it working. Much as I'd like to say I had a breakthrough moment and understand it perfectly, I have no idea how I got it to work. However, in hopes that it might help someone, let me lay out what I did.
I'm running XAMPP on Windows. My file structure has the folder for PHPExcel itself in xampp\php\PEAR\Classes. domPDF is in the same folder, and I renamed it 'dompdf'.
For reasons I no longer recall, I set the include path like so:
ini_set('include_path', ini_get('include_path').'\\Classes\\');
To set the rendering path, I used the following:
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf';
$rendererLibraryPath = ini_get('include_path') . $rendererLibrary;
For the actual writer creation, I'm using the following:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save($path.$fullFileName);
One thing I noticed that may have made things different was doing this:
// include 'PHPExcel/Writer/Excel2007.php';
That is, unlike everything I've done in PHPExcel, I'm not including anything from the Writer folder at all. Best I can remember, that's all that's different this time versus a week ago when I asked the question. Once I'd grabbed the 01simple-download-pdf.php file from the Tests folder in 1.7.8, it was mostly a matter of copying the code from it and tweaking it to my paths.
To summarize, leave $rendererName alone. The $rendererLibrary is the name of the folder that contains the library, 'dompdf' in my case. The $rendererLibraryPath is literally setting the path to that folder, so it ends with the path that contains the pdf library folder.
It should be obvious that I'm no uber-leet hax0r, but SO has answered many, many programming questions for me. I'm hoping this helps someone else, so they're not wasting hours like I did.
The PHPOffice contains also PHPWord. I have had the same error message with PHPWord. This is for LINUX. A replacement of 'PhpWord' by 'PhpExcel' should do the job for this case. You must modify the path $rendererLibraryPath to your needs.
$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF;
$rendererLibraryPath = realpath(__DIR__ . '/../../../../../dompdf-0.6.1');
\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('helloWorld.pdf');
If you are getting this error and you have set the correct path to the TCPDF or DOMPDF folder (you do not have to write the full path), then also make sure you have these lines:
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}

How do Media Views work in cakephp?

I have arranged a file upload to happen on my application relative address: webroot/files
Now I need to force download on the uploaded files. After some googling and trying like most of the suggestions from this post I figured out the correct way to do this is using cakephps Media Views
What I have:
Main site with a table of records. Model -> Record; Table -> records;
These records have a primary key record_id.
In my database I have a Table -> files; Model -> File;
These files have a foreign key record_id and a field 'url' with the relative path to it's location.
After creating a record with files, the files are correctly uploaded to the folder, which relative address is e.g. webroot/files/record_name/file and the tables in database are correctly updated.
What I want to do:
After doubleclicking on one table row open a modal dialog with the information about the record. (done)
In this modal dialog I want to display links that will force download on these files.
I tried many variations of this:
//the retrieving of data after debug looks fine//
$this->loadModel('File');
$files = $this->File->find('list', array(
'conditions'=>array('File.record_id'=>$record_id),
'fields' => array('File.Name', 'File.Url');
))
//actual display of url
foreach($files as $file_name => $file_url) {
echo $this->Html->link($file_name, $file_url);
}
The resulting link looks exactly the way James Revillini presented
This is my actual question
Since that issue was not entirely solved, I thought it would be helpful not only for me, but for anybody who's searching for a quick solution for this problem to see a quick demonstration of how Media-views work. I have no idea where to move after making a dynamic download function:
public function download($name, $path) {
$this->viewClass = 'Media';
$params = array(
'id' => $name,
'name' => $name,
'download' => true,
'path' => $path
);
$this->set($params);
}
Point the link in the modal dialog for the resource to the download() function.
Pass the Record.id to that function. In it find the file and auto-render it.
It should work.