图像始终位于从 pdf 转换的 html 的顶部



我使用以下代码,特定pdf页面的所有内容都以正确的方式转换。但是,如果 pdf 页面中间有任何图像,HTML 中的该图像将显示在顶部。

PHP代码:

umask(0);
$output = shell_exec('pdftohtml create.pdf create.html');

编辑:

请检查我用于此的pdf:https://www.dropbox.com/s/6uy9wq27ff00n0x/create.pdf?dl=0

在此 PDF 中,图像在 2 行之后。

加载转换后的 html 页面。 shell_exec将"s"添加到 html 文件中,创建.html

$html = file_get_contents('creates.html');
print_r($html);

//输出

<!DOCTYPE html><html>
<head>
</head>
<body>
<img src="/var/www/html/pdf-sign/public/converted_path/create-1_1.png"/><br/>
Test document PDF&#160;<br/>&#160;<br/>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est purus, ultrices in porttitor&#160;<br/>in, accumsan non quam. Nam consectetur porttitor rhoncus. Curabitur eu est et leo feugiat&#160;<br/>auctor vel quis lorem. Ut et ligula dolor, sit amet consequat lorem. Aliquam porta eros sed&#160;<br/>velit imperdiet egestas. Maecenas tempus eros ut diam ullamcorper id dictum libero&#160;<br/>tempor. Donec quis augue quis magna condimentum lobortis. Quisque imperdiet ipsum vel&#160;<br/>magna viverra rutrum. Cras viverra molestie urna, vitae vestibulum turpis varius id.&#160;<br/>&#160; &#160;PLACEHOLDER &#160; &#160; &#160;<br/>nulla ac dolor. Maecenas urna elit, tincidunt in dapibus nec, vehicula eu dui. Duis lacinia&#160;<br/>fringilla massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur&#160;<br/>
suscipit felis eget condimentum. Cum sociis natoque penatibus et magnis dis parturient&#160;<br/>montes, nascetur ridiculus mus. Integer bibendum sagittis ligula, non faucibus nulla volutpat&#160;<br/>vitae. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. &#160;<br/>In aliquet quam et velit bibendum accumsan. Cum sociis natoque penatibus et magnis dis&#160;<br/>parturient montes, nascetur ridiculus mus. Vestibulum vitae ipsum nec arcu semper&#160;<br/>adipiscing at ac lacus. Praesent id pellentesque orci. Morbi congue viverra nisl nec rhoncus.&#160;<br/>Integer mattis, ipsum a tincidunt commodo, lacus arcu elementum elit, at mollis eros ante ac&#160;<br/>risus. In volutpat, ante at pretium ultricies, velit magna suscipit enim, aliquet blandit massa&#160;<br/>orci nec lorem. Nulla facilisi. Duis eu vehicula arcu. Nulla facilisi. Maecenas pellentesque&#160;<br/>volutpat felis, quis tristique ligula luctus vel. Sed nec mi eros. Integer augue enim, sollicitudin&#160;<br/>ullamcorper mattis eget, aliquam in est. Morbi sollicitudin libero nec augue dignissim ut&#160;<br/>consectetur dui volutpat. Nulla facilisi. Mauris egestas vestibulum neque cursus tincidunt.&#160;<br/>Donec sit amet pulvinar orci. &#160;<br/>Quisque volutpat pharetra tincidunt. Fusce sapien arcu, molestie eget varius egestas,&#160;<br/>faucibus ac urna. Sed at nisi in velit egestas aliquam ut a felis. Aenean malesuada iaculis nisl,&#160;<br/>ut tempor lacus egestas consequat. Nam nibh lectus, gravida sed egestas ut, feugiat quis&#160;<br/>dolor. Donec eu leo enim, non laoreet ante. Morbi dictum tempor vulputate. Phasellus&#160;<br/>ultricies risus vel augue sagittis euismod. Vivamus tincidunt placerat nisi in aliquam. Cras&#160;<br/>quis mi ac nunc pretium aliquam. Aenean elementum erat ac metus commodo rhoncus.&#160;<br/>
<hr/>
</body>
</html>

现在看

<img src="/var/www/html/pdf-sign/public/converted_path/create-1_1.png"/>

就在身体标签之后。这意味着图像将移至顶部,以取代第三行。

我也遇到了这种问题。我有一个解决方案。首先,您需要将pdf文档转换为XML

$output = shell_exec('pdftohtml -xml create.pdf create.xml');

XML 输出如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pdf2xml SYSTEM "pdf2xml.dtd">
<pdf2xml producer="poppler" version="0.33.0">
<page number="1" position="absolute" top="0" left="0" height="1262" width="892">
<fontspec id="0" size="16" family="Times" color="#000000"/>
<image top="117" left="51" width="424" height="96" src="converted_path/create1.jpg"/>
<text top="57" left="99" width="144" height="16" font="0">Test document PDF</text>
</page>
</pdf2xml>

然后,将此 XML 字符串转换为一个对象

$xml = simplexml_load_string($xmlContent);

之后,您需要使用 xml 属性顶部值测量确切的图像位置,如下所示

$pg = 0;
foreach($xml->page as $page) {
        foreach ($page as $e) {
            $all_attribute[$pg][(int)$e['top']] = $e;
        }
        $pg++;
    }

找出所有属性顶部值后,根据数组[键]对值进行排序

foreach($all_attribute as $page) {
        ksort($page);
}

当所有属性都根据 xml top 值排序时,只需像下面这样处理 html

foreach($xml->page as $page) {
  foreach($page as $p){
      if($p->getName() == 'image'){
         <img width="'.$p['width'].'" height="'.$p['height'].'" src="'.$p['src'].'" >
      }
  }
}

我认为它对你
有帮助您还可以管理文本字体
XML 将所有字体存储在 fontspec 属性中并给出一个 id

<fontspec id="0" size="16" family="Times" color="#000000"/>

并且此ID是文本属性字体值中的调用

<text top="57" left="99" width="144" height="16" font="0">
现在,借助

这些值,您需要处理如下所示的字体

$font = [];
    foreach($xml->page as $page) {
        foreach ($page as $e) {
            if($e->getName() == 'fontspec'){
                $font[(int)$e['id']]['family'] = (string)$e['family'];
                $font[(int)$e['id']]['size'] = (string)$e['size'];
                $font[(int)$e['id']]['color'] = (string)$e['color'];
            }
        }
    }

之后,您需要将此字体处理成 html

foreach($page as $p){
            if($p->getName() == 'text'){
                $ind = (int)$p['font'];
                $font_size = $font[$ind]['size'];
                $font_color = $font[$ind]['color'];
                $font_family = $font[$ind]['family'];
                '<span style="font-size:'.$font_size.'px;color:'.$font_color.';font-family:'.$font_family.'; font-weight: 900;">'.(string)$p.'</span>=';
          }
}
而不是

print_r打印文件的内容:

<?php
echo file_get_contents('creates.html');
?>

并确保这是,并且只有这个在你的 php 中输出。如果你在开头有一些html,它会搞砸布局。

相关内容

  • 没有找到相关文章