yii2tech / html2pdf

Yii2 component for HTML to PDF conversion
Other
78 stars 65 forks source link

Convert Page of Yii2 site #23

Closed vkponomarev closed 4 years ago

vkponomarev commented 4 years ago

Is there are posibility to convert html page that generates from controller/view with css and other like in mPDF

i have page: site.com/page/

how can i generate pdf from this page

mPDF its like this:

class PrintController extends Controller
{
    public function actionPrint()
    {
        $this->layout = "print";
..................
        $render = $this->render('....................', [
           .......................
        ]);
        $mpdf = new Mpdf(
            [
                'mode' => '+aCJK',
                "autoScriptToLang" => true,
                "autoLangToFont" => true,
            ]
        );

        $mpdf->SetMargins(0,0,5);
        $mpdf->SetDisplayMode('fullpage');
        $stylesheet = file_get_contents('........................'); // external css
        $mpdf->WriteHTML($stylesheet,1);
        $mpdf->WriteHTML($render);
        $mpdf->Output();
    }

or u can save it to file.

html2pdf can do like this?

klimov-paul commented 4 years ago

You can convert any string to PDF using \yii2tech\html2pdf\Manager::convert():

class PrintController extends Controller
{
    public function actionPrint()
    {
        $this->layout = "print";

        $html = $this->render('some-view', []);

        Yii::$app->html2pdf
            ->convert($html)
            ->saveAs('/path/to/output.pdf');   
    }

Although it has no sense for me. It is better to use \yii2tech\html2pdf\Manager::render() directly over the view:

class PrintController extends Controller
{
    public function actionPrint()
    {
         Yii::$app->html2pdf
            ->render('some-view')
            ->saveAs('/path/to/output.pdf');   
    }