PDF Generation Results in ERR_INVALID_RESPONSE in Chrome

前端 未结 3 1102
囚心锁ツ
囚心锁ツ 2020-12-10 13:24

When generating a PDF in the browser programmatically (via PHP) the rendered PDF displays fine in both Firefox and Safari, but Chrome returns an ERR_INVALID_RESPONSE. It is

3条回答
  •  悲&欢浪女
    2020-12-10 13:55

    In my case I had to add these 2 parameters to headers because wordpress was sending 404 code as it didn't recognize the url of my php function:

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

    as stated in this answer on wordpress.stackexchange.

    This forces the headers to replace (2nd param true) the 404 status code generated by wordpress as it does not recognize the custom url, and sets 200 OK (3rd param 200).

    So it ended being something like this:

    $pdf_name = "test.pdf";
    $pdf_file = "/absolute/path/to/my/pdfs/on/my/server/{$pdf_name}";
    header('Content-type: application/pdf',true,200);
    header("Content-Disposition: attachment; filename={$pdf_name}");
    header('Cache-Control: public');
    readfile($pdf_file);
    exit();
    

提交回复
热议问题