Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot generate simple PDF #24

Closed
alemedinam opened this issue Oct 13, 2015 · 10 comments
Closed

Cannot generate simple PDF #24

alemedinam opened this issue Oct 13, 2015 · 10 comments

Comments

@alemedinam
Copy link

alemedinam commented Oct 13, 2015

Yii 2.0.6
PHP 5.4.7
Linux mint 17.2

CONTROLLER:

public function actionReporteProgramaPdf()
    {
        // get your HTML raw content without any layouts or scripts
        $content = $this->renderPartial('reporteProgramaPdf');        

        // setup kartik\mpdf\Pdf component
        $pdf = new Pdf([
            // set to use core fonts only
            'mode' => Pdf::MODE_CORE, 
            // LETTER paper format
            'format' => Pdf::FORMAT_LETTER, 
            // 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:18px}', 
             // set mPDF properties on the fly
            'options' => ['title' => 'Krajee Report Title'],
             // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Krajee Report Header'], 
                'SetFooter'=>['{PAGENO}'],
            ]
        ]);

        // return the pdf output as per the destination setting
        return $pdf->render(); 
    }

VIEW:

<?php 
echo'INFORMACIÓN';
?>

RESULT:

%PDF-1.4 %���� 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x��R�N�0����7���N����-�Q�6��b�hZTQ���������/E*�C�䒻�|�l!�����+&�M����n�r/r,��<�`� ���fK\f�������f���z��b�軂=�4� ���o endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> endobj 9 0 obj <> endobj 2 0 obj <> /ExtGState << /GS1 5 0 R >> >> endobj 10 0 obj << /Producer (��mPDF 6.0) /Title (��Krajee Report Title) /CreationDate (20151013165316+02'00') /ModDate (20151013165316+02'00') >> endobj 11 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /XYZ null null 1] /PageLayout /OneColumn >> endobj xref 0 12 0000000000 65535 f 0000000648 00000 n 0000001201 00000 n 0000000015 00000 n 0000000223 00000 n 0000000737 00000 n 0000000798 00000 n 0000000896 00000 n 0000000992 00000 n 0000001093 00000 n 0000001347 00000 n 0000001521 00000 n trailer << /Size 12 /Root 11 0 R /Info 10 0 R /ID [ ] >> startxref 1631 %%EOF

@Rexless
Copy link

Rexless commented Nov 13, 2015

Hi, I have same problem! Did you find a way to solve it?

@alemedinam
Copy link
Author

@Rexless I really don't know what is the problem... you could try with 'destination' => Pdf::DEST_DOWNLOAD. It works for me

@FernandoMauricio
Copy link

i fixed the error, whit 'mode' => Pdf::MODE_UTF8

@almirb
Copy link

almirb commented Aug 2, 2016

Tried MODE_UTF8, but without success...
I solved It putting this code before rendering the PDF, like below:

Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', 'application/pdf');

return $pdf->render();

@dwisuseno
Copy link

yeah, its true. change the mode into 'MODE_UTF8'

@bpanatta
Copy link

FernandoMauricio solution worked for me. Thanks!

@AgnisLV
Copy link
Contributor

AgnisLV commented Sep 17, 2018

The Yii2 manual says that in the default response format "FORMAT_HTML" the "Content-Type" header will be set as "text/html".
My suggestion how to fix it is to modify the file /Pdf.php and add new line at around line 409:
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
as otherwise in newest yii2 version the line

$headers->set('Content-Type', 'application/pdf');
has no effect.

https://www.yiiframework.com/doc/api/2.0/yii-web-response#$format-detail

@JacquesMarques
Copy link

I change the mode into 'MODE_UTF8' but not work, put:

Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', 'application/pdf');

Work, but with this I have to put this in many files, is there another solution?

@kartik-v
Copy link
Owner

kartik-v commented Oct 9, 2018

Check with updated release v1.0.4.

@kartik-v kartik-v closed this as completed Oct 9, 2018
@rajmantechnology
Copy link

We are using this code to download the pdf file but we are getting problem. i want when i hit this route then pdf file will download.
But when i hit this api in postman and click on the download button then pdf file download. So i wnat to download the pdf file with data.

router.get('/demo',function (req,res){
async function getinvoicelist(){

var chunks = [];
var options = { method: 'GET',
url: 'https://api.fortnox.se/3/invoices/31/preview',

request(options, function (error, response, body) {

console.log(body);

});

Image :1
screenshot_36
screenshot_35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants