How to Properly use HTML2PDF and Yii-PDF in Yii? - php

Hello I'm new to yii so currently don't know how to properly use this Html2pdf & yii-pdf extension in yii to get the pdf..
What i actually want.. I have a page called http://localhost/site/Users/Results.. which is showing the list of users..So If i click on user one, it will open a new page called http://localhost/site/Applicant/1
& for user two, it will be
http://localhost/site/Applicant/2
So on these pages there is all the information of users. I want to put a download PDF button on this page, If user clicks on it, He will be able to download all his information in PDF. There can be many users. Everyuser will be able to download all his information in pdf.
I got the html2pdf & yii-PDF .. I got this settings by searching on google, but unable to find proper example to use it according to my requirements above.
config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.extensions.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
/*'defaultParams' => array( // More info: http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:accueil
'orientation' => 'P', // landscape or portrait orientation
'format' => 'A4', // format A4, A5, ...
'language' => 'en', // language: fr, en, it ...
'unicode' => true, // TRUE means clustering the input text IS unicode (default = true)
'encoding' => 'UTF-8', // charset encoding; Default is UTF-8
'marges' => array(5, 5, 5, 8), // margins by default, in order (left, top, right, bottom)
)*/
)
),
),
Here is my controller.
class UsersController extends Controller
{
public function createPDF(){
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
$html2pdf->Output();
}
}
im totally new to yii, so how to use this extension, I have never used any extension before.. is there any other method to download pdf of each user information. any suggestions or and example of working code.

config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.vendors.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
)
),
),
Controller
class UsersController extends Controller
{
public function createPDF(){
$pdffilename = 'test.pdf';
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
ob_clean();
$html2pdf->Output($pdffilename,"I"); // OUTPUT_TO_BROWSER
/* OUTPUT_TO_BROWSER = "I" */
/* OUTPUT_TO_DOWNLOAD = "D" */
/* OUTPUT_TO_FILE = "F" */
/* OUTPUT_TO_STRING = "S" */
}
}

Related

Yii 1.1 pdf extension not showing png images

We have a website that is currently on an Ubuntu 16.04 server and we are working on moving to Amazon. The rest of the site (Written in PHP Yii 1.1) is working fine except for the piece that present our reports in PDF format.
The report is written to pdf with no real issue but images show as a red X After some debugging I came to the conclusion that the majority of the images are png images and that is why they do not show. The single jpg image on the report is showing.
How can I get this to work? The html version of the report (that uses different code) gets the image from the same place and that works just fine. I cannot really convert all images because they are generated dynamically.
I read somewhere that pdf does not support png images, REALLY?? Why did it render just fine on the current server?
I do not believe that is is going to help but here is some code used:
main.config:
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'mpdf' => array(
'librarySourcePath' => 'application.vendors.mpdf.*',
'constants' => array(
'_MPDF_TEMP_PATH' => Yii::getPathOfAlias('application.runtime'),
),
'class'=>'mpdf', // the literal class filename to be loaded from the vendors folder
'defaultParams' => array( // More info: http://mpdf1.com/manual/index.php?tid=184
'mode' => '', // This parameter specifies the mode of the new document.
'format' => 'A4', // format A4, A5, ...
'default_font_size' => 0, // Sets the default document font size in points (pt)
'default_font' => '', // Sets the default font-family for the new document.
'mgl' => 15, // margin_left. Sets the page margins for the new document.
'mgr' => 15, // margin_right
'mgt' => 16, // margin_top
'mgb' => 16, // margin_bottom
'mgh' => 9, // margin_header
'mgf' => 9, // margin_footer
'orientation' => 'P', // landscape or portrait orientation
)
),
),
Controller:
$mPDF1 = Yii::app()->ePdf->mpdf('utf-8', 'A4');
$mPDF1->simpleTables = true;
$mPDF1->useSubstitutions = false;
$mPDF1->WriteHTML($this->renderPartial('pdfRXMapFile', array('model' => $model)));

How to convert yii view page into pdf and download it?

This is my view page that i want to download as pdf. My view link: http://localhost/myproject/Applicant/1
I want to put a download PDF button at the right top of that page, When i click on it, The user profile should be downloaded in PDF. Im new to yii so not sure how to do it, But by searching on google, i found some extensions.
yii-pdf
mpdf1
by following these above extensions, I put those extensions in my protected/extension folder. Then Here is my main/config file..
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'class' => 'WebUser',
'allowAutoLogin'=>true,
'returnUrl'=>'/site/index#login',
'loginUrl'=>array('/Applicant/login'),
),
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'mpdf' => array(
'librarySourcePath' => 'application.extensions.mpdf.*',
'constants' => array(
'_MPDF_TEMP_PATH' => Yii::getPathOfAlias('application.runtime'),
),
'class'=>'mpdf', // the literal class filename to be loaded from the vendors folder
/*'defaultParams' => array( // More info: http://mpdf1.com/manual/index.php?tid=184
'mode' => '', // This parameter specifies the mode of the new document.
'format' => 'A4', // format A4, A5, ...
'default_font_size' => 0, // Sets the default document font size in points (pt)
'default_font' => '', // Sets the default font-family for the new document.
'mgl' => 15, // margin_left. Sets the page margins for the new document.
'mgr' => 15, // margin_right
'mgt' => 16, // margin_top
'mgb' => 16, // margin_bottom
'mgh' => 9, // margin_header
'mgf' => 9, // margin_footer
'orientation' => 'P', // landscape or portrait orientation
)*/
),
),
),
I have created this action in ApplicantController
public function actionPDF()
{
$mPDF1 = Yii::app()->ePdf->mpdf();
$mPDF1->WriteHTML($this->render('UserProfileView',true));
$mPDF1->Output();
}
What should i do next? how to get the button on that view file which is link to controller action to download file.
You need to submit your form to an action.
for example, in your view:
<?php echo CHtml::button('Button Text', array('submit' => array('controller/action'))); ?>
In here the button will call the action in controller and execute what need to be done in that action.
EDITED
If I'm right, you are rendering protected/views/applicant/view.php so you can add in the sidebar the link to you action:
<?php
/* #var $this ApplicantController */
/* #var $model Applicant*/
$this->breadcrumbs=array(
'Applicant'=>array('index'),
$model->id,
);
$this->menu=array(
array('label'=>'List Applicant', 'url'=>array('index')),
array('label'=>'Create Applicant', 'url'=>array('create')),
array('label'=>'Create PDF', 'url'=>array('PDF')),
Don't forget adding to rules in your ApplicantController.php:
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','PDF'),
'users'=>array('*'),

Generate many pdf pages with PHP

I have a problem to generate many pages (more of 5,000 pages) with PHP. In real, the problem is a time of wait to generate.
I am use the framework YII 2.0 and the widget mPDF, to generate 60 pages the time of wait its from 2mins.. I have to otimize this time, but its necessary generate the pdf (with css to style the doc)..
I understand to generate more of 5,000 pages it will take a while, but i need otimize the maximum this time, the way it is now It is too long.
Anyone know the way to do this? Not necessary need to use YII, if have other way with PHP (library, or some way else).
Code:
$content = $this->renderPartial('tabela-pdf', [
'model' => $model,
'table' => $table, //CONTENT
]);
$header = $this->renderPartial('pdf');
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '#vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:14px}',
// set mPDF properties on the fly
'options' => ['title' => 'Relatório'],
'defaultFontSize' => 3,
'marginLeft' => 10,
'marginRight' => 10,
'marginTop' => 30,
'marginBottom' => 11,
'marginHeader' => 6,
'marginFooter' => 6,
// call mPDF methods on the fly
'methods' => [
'SetHeader' => [$header],
'SetFooter' => ['<p style="text-align: center;font-size: 8px;">FOOTER</p>'],
]
]);
$pdfName = 'FILE-_' . date('d-m-Y') . '.pdf';
Yii::$app->response->format = Response::FORMAT_HTML;
Yii::$app->response->setDownloadHeaders($pdfName, 'application/pdf');
return $pdf->render();
Just a document with a big table (no have identical content)

How to upload more than 1 image in fileimagearbehavior extension for Yii

I try to use fileimagearbehavior extension for Yii, and its not a lot documentation for that. In examples I just found how can 1 file be uploaded, but I cant find way how to upload more than 1 file. Here example
class Dish extends CActiveRecord {
public $recipeImg; // used by the form to send the file.
public function rules()
{
return array(
// ...
// for the form too
array('recipeImg', 'file', 'types' => 'png, gif, jpg', 'allowEmpty' => true),
// ...
);
}
public function behaviors() {
return array(
'recipeImgBehavior' => array(
'class' => 'ImageARBehavior',
'attribute' => 'recipeImg', // this must exist
'extension' => 'png, gif, jpg', // possible extensions, comma separated
'prefix' => 'img_',
'relativeWebRootFolder' => 'files/recipes', // this folder must exist
# 'forceExt' => png, // this is the default, every saved image will be a png one.
# Set to null if you want to keep the original format
'useImageMagick' => '/usr/bin', # I want to use imagemagick instead of GD, and
# it is located in /usr/bin on my computer.
// this will define formats for the image.
// The format 'normal' always exist. This is the default format, by default no
// suffix or no processing is enabled.
'formats' => array(
// create a thumbnail grayscale format
'thumb' => array(
'suffix' => '_thumb',
'process' => array('resize' => array(60, 60), 'grayscale' => true),
),
// create a large one (in fact, no resize is applied)
'large' => array(
'suffix' => '_large',
),
// and override the default :
'normal' => array(
'process' => array('resize' => array(200, 200)),
),
),
'defaultName' => 'default', // when no file is associated, this one is used by getFileUrl
// defaultName need to exist in the relativeWebRootFolder path, and prefixed by prefix,
// and with one of the possible extensions. if multiple formats are used, a default file must exist
// for each format. Name is constructed like this :
// {prefix}{name of the default file}{suffix}{one of the extension}
)
);
}
}
And here piece of view:
echo $form->fileField($model,'recipe');

Downloading files using media view in CakePHP

I want to download 4 different files through 4 different links. I am using the Media view to download the files, but I have to hardcode the file name in the download functions in the controller:
function download () {
$this->view = 'Media';
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip',
'path' => APP . 'files' . DS
);
$this->set($params);
}
This works fine for one file. Now, for links number 2,3,4, do I need to create 3 different actions and give different file names in them, or is there a way in which I can use download() to only download the respective file depending on which link has been clicked?
That's what variables are for. Generic example:
function download($fileId) {
$file = // find the file you want to serve based on $fileId
$pathInfo = pathinfo($file['path']);
$this->view = 'Media';
$params = array(
'id' => $file['name'],
'name' => $pathInfo['filename'],
'extension' => $pathInfo['extension'],
'download' => true,
'path' => APP . 'files' . DS
);
$this->set($params);
}
In CakePhp 2.x you will get error The view for YourController::download() was not found.
Use viewClass field in CakePHP 2.x:
$this->viewClass = 'Media';
See Media Views — CakePHP Cookbook v2.x documentation
UPD: Media Views are deprecated since CakePHP 2.3, and CakeResponse::file() should be used:
$this->response->file($file['path'], array('download' => true, 'name' => 'foo'));
return $this->response;

Categories