Forum


Replies: 3   Views: 746
Adding header and footer in trial version
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by lukas-sander  · 11-07-2022 - 09:16

Hello!

I am currently trying out the trial version to check if phpdocx meets my requirements.

I'm trying to add a header and footer to my document, but to no avail. I tried copying the demo header (example 2, the text part), but I couldn't get the script to output anything.

Of course, with the obfuscated files, PhpStorm does not recognize any functions from the WordFragment class, so it might just be that I'm mising something.

Here is my current code:

public function print($fileType = 'docx')
    {

        $json = file_get_contents('php://input');
        $data = json_decode($json,true)['data'];

        $docx = new CreateDocx();
        ob_start();
        $textOptions = array(
            'fontSize' => 13,
            'b' => 'on',
            'color' => '567899',
        );
        $headerText = new WordFragment($docx, 'defaultHeader');
        $headerText->addText('PHPDocX Header Title', $textOptions);
        $docx->addHeader(array('default' => $headerText));


        $docx->embedHTML($data['html']);


        ob_end_clean();
        $docx->createDocx($data['filename']);
        if($fileType === 'pdf') {
            $docx->transformDocument($data['filename'] . '.docx', $data['filename'] . '.pdf');
        }

        header("Content-Type: application/msword");
        header("Content-Disposition: attachment; filename=" . $data['filename'] . "." . $fileType);
        header("Content-Length: " . filesize($data['filename'] . '.' . $fileType));
        header("Content-Transfer-Encoding: binary");
        readfile($data['filename'] . '.' . $fileType);
        ob_end_flush();

        return new Response();
    }

PS: I am aware that I currently do not delete the generated file. I was planning to implement that after I got everything working.

Posted by admin  · 11-07-2022 - 10:18

Hello,

addHeader and addFooter methods are available in the trial package. We recommend you to check and run the samples included in the package (examples folder), all samples are fully tested and working. Please test the samples using phpdocx standalone so you can check they are working correctly.

We have tested the following script that uses your PHP code:

$docx = new CreateDocx();
$textOptions = array(
    'fontSize' => 13,
    'b' => 'on',
    'color' => '567899',
);
$headerText = new WordFragment($docx, 'defaultHeader');
$headerText->addText('PHPDocX Header Title', $textOptions);
$docx->addHeader(array('default' => $headerText));

$docx->embedHTML('<p>HTML content</p>');

$docx->createDocx('output');

And a DOCX with a header is generated correctly. If you don't get any DOCX output please check your server logs, maybe some rw access is missing to generate the DOCX file. If the problem is that your code is not downloading the DOCX, maybe the problem comes from the code you are running to download it. On https://www.phpdocx.com/documentation/cookbook/download-generated-docx you can read the generic instructions to generate and download a DOCX, but if you are using a framework or CMS you may need to use a custom code; for example, if you are using Symfony you need to set the correct Response (https://ourcodeworld.com/articles/read/329/how-to-send-a-file-as-response-from-a-controller-in-symfony-3), or other code if you are using Laravel, Yii, Drupal...

Also note that the transformDocument method is not available in the trial package. This method is only available in Advanced and Premium licenses.

Regards.

Posted by lukas-sander  · 11-07-2022 - 10:55

Thank you for the quick response.

After re-checking, I noticed that the issue is me using Symfony, and thus requiring the namespace loader for additional functions.

I tested the header adding with a regular php file and it worked.