Giter Site home page Giter Site logo

Comments (6)

beroso avatar beroso commented on May 28, 2024 1

You need to refact your format method to something like:

    public function format($response)
    {
        $pdf = Yii::$app->html2pdf->convert($response->data);
        $response->setDownloadHeaders('test.pdf', $mimeType = 'application/pdf', $inline = true);
        $response->content = file_get_contents($pdf->name);
    }

from html2pdf.

beroso avatar beroso commented on May 28, 2024

I think that you forgot the return statement:

return $pdf->send($file, $options = ['mimeType' => 'application/pdf', 'inline'=> false]);

from html2pdf.

Bhoft avatar Bhoft commented on May 28, 2024

Hmm I forgot to mention that i use the class in a response formatter which i have created by myself.
I use this in my controller to get a "export to pdf" possibility for each action by only setting the response formatter when e.g. ?export = pdf is used as a query.

And there is the problem that $pdf->send only works if the file isn't deleted.

something like this:

namespace app\formatter;

use Yii;
use yii\base\Component;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;

class TestResponseFormatter extends Component implements ResponseFormatterInterface
{
    public function format($response)
    {
        //$pdf = Yii::$app->html2pdf->convert('some test');
        $pdf = Yii::$app->html2pdf->convert($response->data);
        $pdf->send('test.pdf', $options = ['inline'=> true]);
    }
}

which that i could render a view in my action and just set the response format. And the html code is used as input for the pdf.

Yii::$app->response->format = 'html2pdf';

in config:

  'response' => [
        'formatters' => [
            'html2pdf' => [
                'class' => 'eurescom\formatter\TestResponseFormatter',
            ],

of course if is save the file (make a copy) i could send it.
I see that this only happens if i use the html2pdf class like above and not directly in the action.

It would be good if there was a possibility to set if the files should be deleted automatically or manually.
I also see no possibility to use my own Tempfile class and just replace the destructor.

from html2pdf.

beroso avatar beroso commented on May 28, 2024

It's clear now. You are misusing the ResponseFormatterInterface.
The format method is suitable to format the response, not send it.

from html2pdf.

Bhoft avatar Bhoft commented on May 28, 2024

you are right. if i send the contents of the file and set the appropriate headers myself it works.
I don't know why i came to the solution to use the send function and why it is working if the file isn't deleted by the destructor.

from html2pdf.

beroso avatar beroso commented on May 28, 2024

You're welcome ;)

from html2pdf.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.