Forum


Replies: 7   Views: 552
Generate word document with headings to be copied and pasted in google docs
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 admin  · 07-01-2023 - 09:27

Hello,

phpdocx includes and applies default styles when adding content. In the case of headings, phpdocx applies the following styles: Heading1PHPDOCX, Heading2PHPDOCX, Heading3PHPDOCX... that are included in the default base template of phpdocx.

Google Docs, and other DOCX editors, have limitations when working with DOCX documents and doesn't support some styles and contents from the OOXML standard used in DOCX documents.

In this case, Google Docs doesn't allow applying custom default style names when contents are copied and pasted, specific custom style names must be used. Instead of mystyle, myheading1, Heading1PHPDOCX or other custom style names, you need to include and use specific style names: Heading1, Heading2...
This requirement comes from Google Docs, not from phpdocx. Google Docs doesn't read heading information correctly from custom paragraph styles, it only allows speficic custom style names. With phpdocx you can use the required custom style names.

On Working with headings you can read all information about working with headings when generating DOCX documents.

In your script you can apply specific custom style names, importing them:

$docx = new CreateDocx();
$docx->importStyles('document_styles_headings.docx', 'merge');

$wordStyles = array('<h1>' => 'Heading1' , '<h2>' => 'Heading2');
$html = '
    <h1>Heading 1</h1>
    <h2>Heading 2</h2>
';
$docx->embedHTML($html, array('wordStyles' => $wordStyles));

or loading the DOCX as a template:

$docx = new CreateDocxFromTemplate('document_styles_headings.docx');

$wordStyles = array('<h1>' => 'Heading1' , '<h2>' => 'Heading2');
$html = '
    <h1>Heading 1</h1>
    <h2>Heading 2</h2>
';
$docx->embedHTML($html, array('wordStyles' => $wordStyles));

or you can also create and the custom paragraph styles using createParagraphStyle.

Following this information, you can use Google Docs preset style names. On https://www.phpdocx.com/en/forum/default/topic/2039 you can read this same question replied in the forum to another user.

Regards.