Forum


Replies: 1   Views: 103
Change filename of streamed pdf or get in memory pdf from transform

Posted by jwphp  · 11-10-2024 - 10:59

When transforming a DOCX to PDF with the native TransformDocAdvPDF I would like to avoid saving to the file system before returning the file to the user from the web app.

One way to do this is to do the transform with 'stream' set to true, but this always sets the filename the user sees in their browser via Content-Disposition to "document.pdf". Is there any way to change this?

Alternatively, is there any way to get an in memory PDF from the transform, which I can then add the relevant Content-Disposition to and return to the client myself?

I am using phpdocx 15.5 Premium.

Posted by admin  · 11-10-2024 - 11:23

Hello,

There's no option in the current stable release of phpdocx to customize the Content-Disposition value when streaming a PDF using TransformDocAdvPDF.

We have added support in the testing branch. Please edit TransformDocAdvPDF.php and replace the following line:

$this->pdf->stream();

with:

$this->pdf->stream($target);

After this change, the target value will be used in Content-Disposition. This same change will be included in the next stable release of phpdocx.

Alternatively, you could use ob_ PHP methods (https://www.php.net/manual/en/ref.outcontrol.php) to get the PDF content and return it as needed:

$docx = new CreateDocx();
CreateDocx::$streamMode = true;
ob_start();
$docx->transformDocument('document.docx', 'document.pdf', 'native', array('dompdf' => $dompdf));
$pdfContent = ob_get_clean();
ob_end_clean();
// custom headers...
echo $pdfContent;
ob_end_flush();

Regards.