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

Add a method to TemplateProcessor for rendering HTML content.Include Image #2547

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Maybe-U
Copy link

@Maybe-U Maybe-U commented Jan 10, 2024

Description

Add a method to TemplateProcessor for rendering HTML content. support image

Fixes 1

@coveralls
Copy link

coveralls commented Jan 10, 2024

Coverage Status

coverage: 97.192% (-0.03%) from 97.217%
when pulling aabbc3c on Maybe-U:feature/render-html
into 41cf4eb on PHPOffice:master.

@Maybe-U Maybe-U changed the title Add a method to TemplateProcessor for rendering HTML content. Add a method to TemplateProcessor for rendering HTML content.Include Image Jan 10, 2024
@oleibman
Copy link
Contributor

@Maybe-U You asked me to review. The code looks okay to me, but I'm really not all that familiar with TemplateProcessor, so it needs someone more knowledgeable to review. However, your test may have a problem. Does it actually attempt to read your external images? If the answer is no, there's no problem. If the answer is yes, I'll add some more comments.

@Maybe-U
Copy link
Author

Maybe-U commented Jan 11, 2024

@Maybe-U You asked me to review. The code looks okay to me, but I'm really not all that familiar with TemplateProcessor, so it needs someone more knowledgeable to review. However, your test may have a problem. Does it actually attempt to read your external images? If the answer is no, there's no problem. If the answer is yes, I'll add some more comments.
@oleibman
Thank you very much for your response. My test cases only verified the generation of the DOCX file and did not check the actual content. However, in reality, it is possible to read external or local images, so my answer is yes.
this is html content

image this is output docx file image @Progi1984 can you review my code thanks you

@Maybe-U Maybe-U closed this Jan 11, 2024
@Maybe-U Maybe-U reopened this Jan 11, 2024
@oleibman
Copy link
Contributor

My testing indicates that your test does attempt to load image https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF and will fail if it is not available; that is probably true of your second image as well. The problem is that external files tend to move or be deleted over time, and, when (not if) that happens, the unit test suite will fail. To avoid this problem, I think you might be able to use AbstractWebServerEmbeddedTest so that the requests are routed to localhost rather than an external site. Again, I'm not all that familiar with it, but there are a lot of tests that use it (e.g. HtmlTest).

@Maybe-U
Copy link
Author

Maybe-U commented Jan 13, 2024

My testing indicates that your test does attempt to load image https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF and will fail if it is not available; that is probably true of your second image as well. The problem is that external files tend to move or be deleted over time, and, when (not if) that happens, the unit test suite will fail. To avoid this problem, I think you might be able to use AbstractWebServerEmbeddedTest so that the requests are routed to localhost rather than an external site. Again, I'm not all that familiar with it, but there are a lot of tests that use it (e.g. HtmlTest).
@oleibman
i am Optimize test cases and handle image loading failures in setHtmlBlock function please have a review thanks!

if (!empty($imageSrcList)) {
foreach ($imageSrcList as $imageSrc) {
try {
$content = file_get_contents($imageSrc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of the code is quite clever, but (a) I'm not really sure it's needed, and (b) I don't think you've coded it correctly. I would personally eliminate all the code after addSection and before addHtml and just let it fail later if the file isn't available; your changes to your test member are sufficient to address my original concern by making the files local rather than external. But, I can see that this checking might be perceived as a benefit, so let's discuss (b). file_get_contents does not normally throw an exception (I am not expert in the Php internals, so I suppose there might be some edge case where it throws); it normally just returns false if it fails and issues some warning messages describing the failure. So, I think what you want to do is:

foreach ... {
    $content = @file_get_contents(...); // suppress warning messages
    if ($content === false) {
        $localImg = ...
        $htmlContent = ...
    }
}

Copy link
Author

@Maybe-U Maybe-U Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks you for your Suggestion,I wasn't thoughtful enough ,I reviewed the html::add html method,Use @ to suppress errors when processing images in this method
image
so i eliminate all the code after addSection and before addHtml Perhaps the abnormal picture should be handled by the user itself.

In addition, this is my first time to submit pr. Could you please help me merge this pr
@oleibman

githubguoyy

This comment was marked as abuse.

@majeeed87
Copy link

<w:sectPr><w:pgSz w:orient="portrait" w:w="11905.511811023622" w:h="16837.79527559055"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:num="1" w:space="720"/></w:sectPr>

Due to the section's properties are stored in the sectPr element above. The document style changed.
So, I use this line to delete the sectPr element before replaceXmlBlock.

$documentBodyStr = preg_replace('/<w:sectPr>.*?<\/w:sectPr>/', '', $documentBodyStr);
//replace document.xml
$this->replaceXmlBlock($search, $documentBodyStr, 'w:p');

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

Successfully merging this pull request may close these issues.

5 participants