Forum


Replies: 7   Views: 724
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 joegraisbery  · 07-01-2023 - 05:52

when i copy content from generated word document and paste in google docs then i am facing formating issues like heading h1,h2 ect. not working in google docs. heading display like normal text.

Posted by joegraisbery  · 07-01-2023 - 06:43

hi any one can help me to resolve this issue. i am using basic version of phpdocx

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.

Posted by joegraisbery  · 09-01-2023 - 08:11

hi

i tried below code but it not working

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

i am generating docx from html and html is dynamic there is no spacific format for docx. how can i create template and define style for heading

Posted by admin  · 09-01-2023 - 08:26

Hello,

All sample codes are fully tested and working.

As explained in our previous reply, phpdocx doesn't include Heading1, Heading2, Heading3 as custom paragraph style names in the default base template. If you need to use these custom paragraph style names you must import them from an external DOCX that includes them (generated with MS Word or any other DOCX editor) or use a custom template that includes them or create them.

Please read our previous reply that details everything about this question.

Also, please note that phpdocx includes default CSS styles applied when transforming HTML. These default CSS styles can be changed applying new CSS styles when importing HTML.

Regards.

Posted by joegraisbery  · 09-01-2023 - 09:25

hi

i created word file custom_template.docx in microsoft word and upload that in server and performed bellow code

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

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

still it's not working i also tried below code

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

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

 

Posted by admin  · 09-01-2023 - 09:32

Hello,

Does your DOCX contain the custom styles you want to use? To include custom styles in a DOCX with MS Word is not enough to generate an empty DOCX. Contents with the needed styles must be added, otherwise MS Word won't include them in the DOCX.

For futher support, please send the custom_template.docx file you are using to contact[at]phpdocx.com and we'll check it.

Regards.

Posted by joegraisbery  · 09-01-2023 - 10:46

Thanks

now it's working. i added some content in docx template file with needed styles and now it's working