Forum


Replies: 7   Views: 1128
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  · 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.