Closed Bhoft closed 6 years ago
I think that you forgot the return statement:
return $pdf->send($file, $options = ['mimeType' => 'application/pdf', 'inline'=> false]);
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.
It's clear now. You are misusing the ResponseFormatterInterface
.
The format method is suitable to format the response, not send it.
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);
}
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.
You're welcome ;)
Hi i have created a pdf from a view. The pdf is correctly rendered.
How can i "send" the file? I tried:
But always getting an error. The problem is that the Tempfile destructor deletes the file automatically on php end. https://github.com/yii2tech/html2pdf/blob/master/src/TempFile.php#L36
So that the File couldn't been send because it's already deleted.
If i remove that line the file sending works flawlessly.
Wouldn't at be better to delete the file after download?
Of course i could create another copy of that file, but then i don't understand the functions to send that file.
But maybe there is another solution which i don't know to prevent the call of the
__destruct
call.