Php 如何在PDF中正确定位图像旁边的文本?

Php 如何在PDF中正确定位图像旁边的文本?,php,css,image,smarty,tcpdf,Php,Css,Image,Smarty,Tcpdf,我正在使用PHP、Smarty和TCPDF库生成文档的PDF副本。 该文档包含来自WIRIS编辑器的数学表达式图像以及文本内容 我在正确定位表达式图像旁边的文本时遇到问题 我尝试了CSSfloatproperties中的每一件事,但什么也没发生。我附上了我想要的这封邮件的截图 这是用于打印问题及其选项的smarty模板代码: {foreach from=$question_data item=qstn_ans key=key} <table border="0" width="10

我正在使用PHP、Smarty和TCPDF库生成文档的PDF副本。 该文档包含来自WIRIS编辑器的数学表达式图像以及文本内容

我在正确定位表达式图像旁边的文本时遇到问题

我尝试了CSS
float
properties中的每一件事,但什么也没发生。我附上了我想要的这封邮件的截图

这是用于打印问题及其选项的smarty模板代码:

{foreach from=$question_data item=qstn_ans key=key}
    <table border="0" width="100%" cellpadding="2" cellspacing="0">
        <tr>
            <td valign="top" >{if $qstn_ans.question_directions}{$qstn_ans.question_directions}<br /><b>Question {$que_seq_no} : </b>{/if}{$qstn_ans.question_text}</td>
        </tr>
        {if $qstn_ans.question_file}
        <tr>
            <td><img src="{$ques_thum_image_path}{$qstn_ans.question_id}_{$qstn_ans.question_file}" /></td>
        </tr>
        {/if}
        {if $qstn_ans.question_has_sub_ques==0}
            {if $qstn_ans.answer}
                {foreach from=$qstn_ans.answer item=ans key=ans_no}
                    <td valign="top" >
                        {if $ans.answer_is_right==1}{assign var='correct_ans' value=$ans_no+1}{/if}
                            <b>{$ans_no+1}.</b>&nbsp;&nbsp;{if $ans.answer_text!=''}{$ans.answer_text}{/if}
                            {if $ans.answer_file!=''}<img src="{$ans_thumb_img_path}{$ans.answer_id}_{$ans.answer_file}" />{/if}
                     </td>
                </tr>
                {/foreach}
        <tr>
            <td></td>
        </tr>
    </table>
{/foreach}
{foreach from=$question\u data item=qstn\u ans key=key}
{if$qstn_ans.question_directions}{$qstn_ans.question_directions}
问题{$que_seq_no}:{/if}{$qstn_ans.question_text} {if$qstn_ans.question_file} {/if} {if$qstn_ans.question_has_sub_ques==0} {if$qstn_ans.ans} {foreach from=$qstn_ans.answer item=ans key=ans_no} {if$ans.answer_is_right==1}{assign var='correct_ans'value=$ans_no+1}{/if} {$ANSUNO+1}。{if$ans.answer\u text!=''}{$ans.answer\u text}{/if} {if$ans.answer_文件!=''}{/if} {/foreach} {/foreach}
这个代码片段可能包含一些错误,因为我在没有检查循环和括号完成的情况下随机粘贴了它,但这不是问题所在

此代码中唯一重要的部分是打印问题文本和问题图像(如果存在)的行

我研究了正确的解决方案,但没有得到想要的。有人能帮我吗


发现此代码工作正常

$html = <<<EOD
<h1><img src="http://www.google.com/logos/2013/wangari_maathai_73rd_birthday-1400005-hp.jpg" height="50px" width="150px"> Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text <img src="http://www.google.com/logos/2013/wangari_maathai_73rd_birthday-1400005-hp.jpg" height="50px" width="150px"> <br>is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;

// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

$html=尝试使用

代码来解决您的问题

似乎所有不一致的信息都来自
$qstn\u ans.question\u text
,因此上面的代码实际上没有问题

我建议将该问题案文中的每一项“声明”另存一段。例如

<p>Statement I: The variance of first n even natural numbers is <img src="?" /></p>
<p>Statement II: The sum of first n natural numbers is <img src="?" /></p>
<p>and the sum of squares of first n natural numbers is <img src="?" /></p>
语句一:前n个偶数自然数的方差为

报表二:前n个自然数之和为

前n个自然数的平方和是


这应该会导致问题文本的每一行单独显示,而不会相互碰撞。

我同意Nerdwood的观点,这看起来不是一个聪明的问题;我认为这只是一个CSS问题。
img
向下延伸的距离足以抓住下一行文本的左边缘。您需要向下撞击文本,使其可以向左移动。您可以在
img
上放置CSS属性,如
clear:right


我认为应该这样做。

TCPDF的HTML不是完美的,所以您应该在每个
中使用
宽度
属性:

。。。
{if$qstn_ans.question_directions}{$qstn_ans.question_directions}
问题{$que_seq_no}:{/if}{$qstn_ans.question_text} {if$qstn_ans.question_file} {/if} ...
如果这不起作用,你可以试试clearfix黑客--
img:after{…}
等等。
...
<tr>
    <td valign="top" width="250" >
        {if $qstn_ans.question_directions}{$qstn_ans.question_directions}<br /><b>Question {$que_seq_no} : </b>{/if}{$qstn_ans.question_text}
    </td>
</tr>
    {if $qstn_ans.question_file}
<tr>
    <td width="100">
        <img src="{$ques_thum_image_path}{$qstn_ans.question_id}_{$qstn_ans.question_file}" />
    </td>
</tr>
    {/if}
...