0

I have Yii2-advanced project where I can download PDF. Everything works great on PC but on mobile devices download is creating .html extension like "example.pdf.html". I tried to add header at the beginning of function but still have no success. I know that there must be header like

header("Content-type:application/pdf");

who tells what kind of file it will be, but don't know where is the problem and why it is still downloading wrong file name.

Here is example of code:

public function run()
    {
        header('Content-Type', 'application/pdf');
        if (strpos($this->type, 'e') === 0) {
            $model = new DataModel($this->id);

            try {
                if (Yii::$app->request->get('button') === Yii::t('frontend', 'Print')) {
                    $pdf = $model->pdf();
                    $pdf->Output("{$model->id}.pdf", 'D');
                    return null;
                }
            } catch (MpdfException $e) {
                Yii::error($e->getMessage());
            }
            $this->type = self::TYPE_EXAMPLE;
            return $this->render($this->type, ['model' => $model]);
        }

        $model_ids = explode(',', $this->id);
        $model_ids = array_unique(array_filter(array_map('trim', $model_ids)));

        $models = [];
        foreach ($model_ids as $id) {
            $models[] = new DataModel($id);
        }

        try {
            if (Yii::$app->request->get('print') !== null) {
                $model = new DataModel(Yii::$app->request->get('print'));
                $pdf = $model->pdf();
                $pdf->Output("{$model->id}.pdf", 'D');
                return null;
            }
            if (Yii::$app->request->get('button') === Yii::t('frontend', 'Print all')) {
                $pdf = DataModel::array_pdf($models);
                $pdf->Output("{$this->id}.pdf", 'D');
                return null;
            }

        } catch (MpdfException $e) {
            Yii::error($e->getMessage());
        }

        $this->type = self::TYPE_EXAMPLE_2;
        return $this->render($this->type, [
            'models' => $models,
            'ids' => $this->id]);
    }

I know that if I am using "dest" D in pdf Output method it should set header, but still it don't work. Can anyone help me with this problem?

Karlis Pokkers
  • 224
  • 5
  • 18
  • Try adding a `Content-Disposition` header that specifies the file name (https://stackoverflow.com/a/7263943/1427878, https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition), and see if that changes anything. – CBroe Nov 11 '22 at 14:07

0 Answers0