Forum


Replies: 3   Views: 869
Top border missing from image
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 RDI  · 01-11-2021 - 22:44

When using addImage with a border the top border is getting cut-off / not rendered.

If I export to PDF the top border is still missing, even in the PDF.

Going into "Format Picture" in Word and changing anything (like moving Transparency from 0% to 1%) causes the top border to then get rendered.

So it seems like the code getting generated by phpdocx is missing something or is incorrect in some way that gets fixed by Word when the image is edited. The problem is the Word doc I'm generating has dozens of images that all have this problem.

Here is simple code that demonstrates the problem.

<?php

require_once $_SERVER["DOCUMENT_ROOT"] . '/includes/phpdocx-12/classes/CreateDocx.php';

$docx = new CreateDocx();

$docx->modifyPageLayout(
        'letter',
        array(
                'marginLeft' => round(1440 * 0.75),
                'marginRight' => round(1440 * 0.75)
        )
);

$img = 'testDocx.jpg';

$width = 600;
$height = 600;

$docx->addText(
                'Heading Text',
                array(
                        'fontSize' => 10,
                        'textAlign' => 'left',
                        'bold' => true,
                        'caps' => true,
                        'keepLines' => true,
                        'keepNext' => true
                )
        );

$imgOptions = array(
        'src' => $img,
        'spacingTop' => 0,
        'spacingBottom' => 0,
        'spacingLeft' => 0,
        'spacingRight' => 0,
        'textWrap' => 0,
        'imageAlign' => 'center',
        'width' => $width,
        'height' => $height,
        'borderColor' => 'AAAAAA',
        'borderStyle' => 'solid',
        'borderWidth' => 1,
);
$docx->addImage($imgOptions);        

$docx->createDocxAndDownload('test');

?>

The image is an 1800 x 1800 screen capture of a map (I didn't see a way to attach it) that gets scaled to 600 x 600.

Thanks

Posted by admin  · 02-11-2021 - 06:58

Hello,

The code you are running is setting spacing styles to 0:

'spacingTop' => 0,
'spacingBottom' => 0,
'spacingLeft' => 0,
'spacingRight' => 0,

and the paragraph overlaps the top border. When you open the document with MS Word and do minor change to the image, MS Word changes spacing values (it is removing 0 top and bottom spacings and adding other values).

phpdocx follows the OOXML standard and a 0 value applied to image spacings is valid too (and useful for specific cases). If you want to display the borders in all cases, without being overlapped by other contents, we recommend you apply a non 0 spacing value, for example:

'spacingTop' => 2,
'spacingBottom' => 2,
'spacingLeft' => 0,
'spacingRight' => 0,

Regards.

Posted by RDI  · 02-11-2021 - 14:34

Thanks, that worked. I had tried setting the top border to 1 and 2, but not the bottom border.

Posted by admin  · 05-11-2021 - 17:03

Hello,

You only need to set the spacingBottom option if you want to add spacing in that specific position. In your sample, it's enough adding a value to spacingTop.

Regards.