Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML to Docx - Text Styles Not Parsing - With a solution #415

Closed
mogilvie opened this issue Nov 9, 2014 · 6 comments
Closed

HTML to Docx - Text Styles Not Parsing - With a solution #415

mogilvie opened this issue Nov 9, 2014 · 6 comments

Comments

@mogilvie
Copy link

mogilvie commented Nov 9, 2014

Hi,

For assistance to others. If your inline html style changes are not being carried over to your docx document, it is likely due to the HTML parseStyle method.

The \PhpWord\Shared\HTML.php parseStyle method doesn't handle inline font size or color style changes.

Adding the styles you want to change is relatively easy:

    /**
     * Parse style
     *
     * @param \DOMAttr $attribute
     * @param array $styles
     * @return array
     */
    private static function parseStyle($attribute, $styles)
    {
        $properties = explode(';', trim($attribute->value, " \t\n\r\0\x0B;"));
        foreach ($properties as $property) {
            list($cKey, $cValue) = explode(':', $property, 2);
            $cValue = trim($cValue);
            switch (trim($cKey)) {
                case 'text-decoration':
                    switch ($cValue) {
                        case 'underline':
                            $styles['underline'] = 'single';
                            break;
                        case 'line-through':
                            $styles['strikethrough'] = true;
                            break;
                    }
                    break;
                case 'text-align':
                    $styles['align'] = $cValue;
                    break;
                // added to handle font size changes.
                case 'font-size':
                    $styles['size'] = substr( $cValue, 0, -2); // substr used to remove the px from the html string size string
                    break;
                 // added to handle color changes.
                case 'color':
                    $styles['color'] = trim($cValue, "#"); //must use hex colors
                    break;
                case 'background-color':
                    $styles['bgColor'] = trim($cValue, "#"); //must use hex colors
                    break;
            }
        }

        return $styles;
    }

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@mogilvie mogilvie changed the title HTML to Docx - Text Styles Not Parsing HTML to Docx - Text Styles Not Parsing - with a solution Nov 11, 2014
@mogilvie mogilvie changed the title HTML to Docx - Text Styles Not Parsing - with a solution HTML to Docx - Text Styles Not Parsing - With a solution Nov 11, 2014
@KRav007
Copy link

KRav007 commented Jun 22, 2016

this is not working with the current version - do someone have hints for it?

@gfiore88
Copy link

It seems doesn't work... any solution?

@troosan
Copy link
Contributor

troosan commented Feb 13, 2018

this should not be necessary anymore as it was implemented in release 0.13
@gfiore88 what's the HTML you are are trying to parse?

@troosan troosan closed this as completed Feb 13, 2018
@lrigdon
Copy link

lrigdon commented Feb 13, 2018 via email

@troosan
Copy link
Contributor

troosan commented Feb 13, 2018

@lrigdon sure, if it makes the library better !

@lrigdon
Copy link

lrigdon commented Feb 14, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants