Forum


Replies: 7   Views: 1794
Problem with inline style
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 giampo  · 18-07-2020 - 20:51

Hi, 

we've a phpdocx premium license and we have a problem with some functions. 

when we use the customizeWordContent funcionts the inline elements of source docx file are not overwritten.

Let's me explain better.

We have to process with phpdocx a docx file created by a user with microsoft word 2013-2019.  In this file we have to change font globally and set a font size, keeping bold and italic style of the source file. 

If the source file already has a global class, i.e. the same font for the whole file, the same size, everything seems to work but for example if there are two different fonts in the same file it seems that word assigns certain inline style and phpdocx cannot overwrite this and set globally the new desired style 

how should we do? there is a way to assign a new default style to whole file overwriting inline style keeping bold italics etc.

Please let me know. thanks

Posted by admin  · 19-07-2020 - 13:32

Hello,

phpdocx can overwrite all styles, but the type to be customized may change depending on the style. For example, a font size or a font family can be applied as global style (setDefaultFont or setDocumentDefaultStyles methods), as a custom paragraph/character style (DocxCustomizer/sample_9.php example), as a paragraph style (DocxCustomizer/sample_1.php example) or as a run style (DocxCustomizer/sample_8.php example).

If some style is not being changed is because the correct target/type (https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP target and type options) are not being selected.

Please check the samples included in examples/DocxCustomizer. If you post the code you are using to change the styles we'll check it.

Regards.

Posted by giampo  · 19-07-2020 - 14:08

It seem to be not working. Phpdocx can only change color of text. But not font. We have tried all of your suggestions with no result. What can i do? 

Posted by admin  · 19-07-2020 - 14:34

Hello,

phpdocx can change all styles explained on https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP , not only the color of the text. As requested in our previous reply, please post the code you are using to change the styles so we can check it.

If you attach the DOCX (using a link to the document) or send it to contact[at]phpdocx.com we'll generate a custom sample to illustrate how to do it using your DOCX.

Regards.

Posted by giampo  · 20-07-2020 - 07:59

Original DOCX:
https://www.topport.it/media/8b897b82-89f5-48e3-8f9e-3d0bce905d93/ArticoloClelia.docx

My code:

$docxFinal = new CreateDocx();
$docxFinal->importContents('ArticoloClelia.docx', ['type' => 'paragraph']);
$docxFinal->embedFont('fonts/Roboto/roboto.ttf', 'Roboto');
$docxFinal->embedFont('fonts/Roboto/robotoi.ttf', 'Roboto', ['styleEmbeding' => 'Italic']);
$docxFinal->embedFont('fonts/Roboto/robotob.ttf', 'Roboto', ['styleEmbeding' => 'Bold']);
$docxFinal->embedFont('fonts/Roboto/robotobi.ttf', 'Roboto', ['styleEmbeding' => 'BoldItalic']);
$docxFinal->setDefaultFont('Roboto');

$defaultStyle = [
    'color' => '000000',
    'italic' => false,
    'bold' => false,
    'font' => 'Roboto',
    'fontSize' => (12 * 2),
    'textAlign' => 'both',
    'lineSpacing' => 360
];
$docxFinal->setDocumentDefaultStyles($defaultStyle);

$docxFinal->customizeWordContent(
    [ 'type' => 'style', 'target' => 'style', 'customAttribute' => '//w:body/w:r[1=1 and descendant::w:rFonts[contains(@w:ascii, "*")]]' ],
    [
        'color' => 'FF0000',
        'bold' => false,
        'italic' => false,
        'font' => 'Roboto',
        'fontSize' => 12,
        'lineSpacing' => 360,
        'spacingTop' => 90,
        'textAlign' => 'both'
    ]
);

$docxFinal->customizeWordContent(
    [ 'type' => 'style', 'target' => 'style', 'attributes' => ['w:*' => ['w:val' => 'ContenutoArticolo']] ],
    [
        'color' => '000000',
        'bold' => false,
        'italic' => false,
        'font' => 'Roboto',
        'fontSize' => 12,
        'lineSpacing' => 360,
        'spacingTop' => 90,
        'textAlign' => 'both'
    ]
);

$docxFinal->customizeWordContent(
    [ 'type' => 'style', 'target' => 'style', 'attributes' => ['w:*' => ['w:val' => 'TitoloCapitolo']] ],
    [
        'bold' => true,
        'italic' => false,
        'color' => '000000',
        'font' => 'Roboto',
        'fontSize'=> 14,
        'lineSpacing' => 240,
        'spacingTop' => 360,
        'spacingBottom' => 240,
        'textAlign' => 'both'
    ]
);

$docxFinal->customizeWordContent(
    [ 'type' => 'style', 'target' => 'style', 'attributes' => ['w:*' => ['w:val' => 'TitoloSottoCapitolo']] ],
    [
        'bold' => false,
        'italic' => true,
        'color' => '000000',
        'font' => 'Roboto',
        'lineSpacing' => 240,
        'textAlign' => 'both'
    ]
);

$docxFinal->customizeWordContent(
    [ 'type' => 'style', 'target' => 'style', 'attributes' => ['w:*' => ['w:val' => 'NoteArticolo']] ],
    [
        'color' => '000000',
        'font' => 'Roboto',
        'fontSize'=> 8,
        'lineSpacing' => 240,
        'textAlign' => 'both'
    ]
);

$docxFinal->modifyPageLayout('A4',[
    'marginTop' => $this->cm2twips(3.5),
    'marginBottom' => $this->cm2twips(3.5),
    'marginLeft' => $this->cm2twips(4.5),
    'marginRight' => $this->cm2twips(4.5),
]);

$docxFinal->createDocx('ArticoloClelia_final.docx');

Final document:
https://www.topport.it/media/8b897b82-89f5-48e3-8f9e-3d0bce905d93/ArticoloClelia_final.docx

Note:
The script change color of text but not the font. I tried to change the "type" and "target" in all possible values but no changes.

Posted by admin  · 20-07-2020 - 09:13

Hello,

Running the following code Roboto font is applied to the text contents in the document body correctly (that uses a very similar code than the samples included in the package):

$docxFinal = new CreateDocx();

$docxFinal->importContents('ArticoloClelia.docx', ['type' => 'paragraph']);
$docxFinal->embedFont('fonts/Roboto/roboto.ttf', 'Roboto');
$docxFinal->embedFont('fonts/Roboto/robotoi.ttf', 'Roboto', ['styleEmbeding' => 'Italic']);
$docxFinal->embedFont('fonts/Roboto/robotob.ttf', 'Roboto', ['styleEmbeding' => 'Bold']);
$docxFinal->embedFont('fonts/Roboto/robotobi.ttf', 'Roboto', ['styleEmbeding' => 'BoldItalic']);
$docxFinal->setDefaultFont('Roboto');

$defaultStyle = [
    'font' => 'Roboto',
];
$docxFinal->setDocumentDefaultStyles($defaultStyle);

foreach (array('paragraph', 'run', 'list', 'style') as $type) {
        $docxFinal->customizeWordContent(['target' => 'document', 'type' => $type], ['font' => 'Roboto']);
}

$docxFinal->createDocx('output.docx');

We have checked the output with MS Word 2007, MS Word 2010, MS Word 2013, MS Word 2016, MS Word 2019, LibreOffice 5 and LibreOffice 6 and in all cases Roboto is applied to all existing contents in the document body content.

Also please note that your code has some errors, for example:

  • 'attributes' => ['w:*'

w:* is not a valid attribute, you need to set a specific attribute name, not a wildcard, to get the correct attribute to be changed.

  • [ 'type' => 'style', 'target' => 'style', 'customAttribute' => '//w:body/w:r[1=1 and descendant::w:rFonts[contains(@w:ascii, "*")]]' ]

you are trying to change the style target (style file) with the customAttribute //w:body/w:r that is a tag (not an attribute) that only exists in the document.xml file.

Also please note that your DOCX contains footnotes that aren't supported in the current version of DOCXCustomizer (https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP):

target  string  document (default), style, lastSection, header, footer.

Regards.

Posted by giampo  · 20-07-2020 - 10:10

ok thanks, now it works but on the documentation the wildcard seems to be accepted so we use that.
For the footnotes what can we fix? how can we use them correctly?

Posted by admin  · 20-07-2020 - 10:20

Hello,

Only the type option allows a wildcard, as the documentation details:

type  string  * (all), break, image, list, paragraph, run, section, style, table, table-row, table-cell.

The attributes option needs a specific key and value:

attributes  array  Contains a specific attribute key and value.

Sorry but as explained in our previous reply, footnotes aren't supported yet as target with customizeWordContents. These are the supported targets:

target string document (default), style, lastSection, header, footer

Supporting other targets such as footnotes and comments with customizeWordContents are a work in progress that isn't yet ready to be used in production servers.

Regards.