比如:
 <div>
        <font color="red">文字信息</font>
<br>
        <font>换行</font>
 </div>这样一段HTML代码,如何保持他的格式,生成一张图片

解决方案 »

  1.   

    可能的现成途径:
    1、使用 java 的 html2image
    2、使用 TCPDF 生成 pdf 然后...
    3、使用 php 的截屏函数 imagegrabscreen、imagegrabwindow
      

  2.   

    我补充一个
    https://zh.wikipedia.org/wiki/Webkit
    https://en.wikipedia.org/wiki/Webkit
      

  3.   

    楼上的连接无法访问还可以用 dompdf
    先 define('DOMPDF_PDF_BACKEND', 'gd'); 就可生成图片
      

  4.   

    示例define('DOMPDF_PDF_BACKEND', 'gd');
    require_once("dompdf_config.inc.php");$html =<<< HTML
    <meta http-equiv="Content-Type" content="text/html; charset=gbk">
    <div>
            <font color="red">文字信息</font>
    <br>
            <font>换行</font>
     </div>
    HTML;$dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream('');
      

  5.   

    dompdf 的下载地址
    http://code.google.com/p/dompdf/downloads/list
      

  6.   

    不同版本的 dompdf 会有不同的表现(我这是 dompdf.php,v 1.24 2009-04-29 的)
    它使用 DOMDocument 解析 html
    print_r($dompdf->_xml);
    可以看到解析是否正常
      

  7.   


    看到提示错误了
    Fatal error: Cannot access protected property DOMPDF::$_xml in G:\AppServ\www\6\1.php on line 16
      

  8.   

    你的php版本是多少?_xml 属性的确是保护模式的
    dompdf.cls.php 中有 
    class DOMPDF {
      protected $_xml;但我在 php5.4.12 中依然可以看到
    DOMPDF Object
    (
        [_xml:protected] => DOMDocument Object
            (
                [doctype] => (object value omitted)
    ....你可以将他改为 public 模式的
      

  9.   

    谢谢版主热心回答
    我的php版本是  PHP Version 5.2.6 
    我下载的dompdf是 @version $Id: dompdf.cls.php 468 2012-02-05 10:51:40Z fabien.menager  应该是最新的
      

  10.   

    我把xml改成public 里 现在提示错误DOMDocument Object ( ) 
    Warning: Cannot modify header information - headers already sent by (output started at G:\AppServ\www\6\1.php:16) in G:\AppServ\www\6\include\gd_adapter.cls.php on line 736Warning: Cannot modify header information - headers already sent by (output started at G:\AppServ\www\6\1.php:16) in G:\AppServ\www\6\include\gd_adapter.cls.php on line 751
      

  11.   

    看样子是对ssl 也下手了,去掉http后的s应该能访问,webkit也不是什么关键词
    http://zh.wikipedia.org/wiki/Webkit
    http://en.wikipedia.org/wiki/Webkit不过 PHP 好像也没webkithttp://www.phpclasses.org/package/7460-PHP-Convert-HTML-to-PDF-using-the-Webkit-engine.html
    看看这个有没帮助
      

  12.   

    你下载的 dompdf 本身应该是没有问题的(这一点你可以通过生成 pdf 加以验证)
    由于 dompdf 提供了生成图片的功能,所以也只是在正确生成 pdf 的基础上,通过定义 DOMPDF_PDF_BACKEND 常量将其图片生成的功能调出来而已
    至于在测试时修改它的部分代码,这也是出于测试、分析的需要。也当然是自己对修改产生的后果负责
      

  13.   

    可否把你的版本发出来测试一下,用的0.5.2这个版本的测试了,没效果,图片没显示,也没发现有xml支持的
      

  14.   

    不报错似乎不应该
    gd写文字总是需要字体文件的
    dompdf的默认字体是 Times-Roman
    为此作为测试,你可以将任意一款ttf中文字体复制到 lib/fonts 目录中,并更名为 Times-Roman.ttf版本声明<?php
    /**
     * DOMPDF - PHP5 HTML to PDF renderer
     *
     * File: $RCSfile: dompdf.php,v $
     * Created on: 2004-06-22
     *
     * Copyright (c) 2004 - Benj Carson <[email protected]>
     *
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or (at your option) any later version.
     *
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public License
     * along with this library in the file LICENSE.LGPL; if not, write to the
     * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     * 02111-1307 USA
     *
     * Alternatively, you may distribute this software under the terms of the
     * PHP License, version 3.0 or later.  A copy of this license should have
     * been distributed with this file in the file LICENSE.PHP .  If this is not
     * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
     *
     * The latest version of DOMPDF might be available at:
     * http://www.digitaljunkies.ca/dompdf
     *
     * dompdf.php is a simple script to drive DOMPDF.  It can be executed from
     * a browser or from the command line.
     *
     * @link http://www.digitaljunkies.ca/dompdf
     * @copyright 2004 Benj Carson
     * @author Benj Carson <[email protected]>
     * @package dompdf
     * @version 0.5.1
     *//* $Id: dompdf.php,v 1.24 2009-04-29 04:11:35 benjcarson Exp $ */
      

  15.   

    php版本我更改为php5.4.18了,xml可以正常了
    可还是无法显示图片,还是打叉的,我也下的是你上面这个版本* @version 0.5.1了
      

  16.   

    不过时间是有点区别* @version 0.5.1
     *//* $Id: dompdf.php,v 1.17 2006/07/07 21:31:02 benjcarson Exp $ */是1.17的,不是1.24的,你能把你这个1.24的打包发出来测试下不?
      

  17.   

    已上传 http://download.csdn.net/detail/xuzuning/5958493
      

  18.   

    我用了0.6那个版本+php5.2.0可以显示图片了,不过没有显示全部,中文部分没显示定义的数字也没显示
      

  19.   

    由于 dompdf 使用 DOMDocument 解析 HTML
    所以被解析的 HTML 文档中一定要有语言声明
    <meta http-equiv="Content-Type" content="text/html; charset=编码">
    否则将按欧洲语言解析,汉字就不能显示了
      

  20.   

    使用 php 的截屏函数 imagegrabscreen、imagegrabwindow这2个函数有谁使用过的没?
      

  21.   

    好像不能随便换字体,你这个貌似跟直接在google下载的有点不一样,要改字体,好像还得改程序
      

  22.   

    截屏函数只用于 window 系统
      

  23.   

    刚才下了最新的 0.6
    @version $Id: dompdf.php 448 2011-11-13 13:00:03Z fabien.menager $补上字体文件 Times-Roman.ttf
    #6 的代码即可正常运行
      

  24.   

    是的,dompdf 的渲染效果很差不过0.6也改进了不少
    但比起 tcpdf 来就差的太远了,只是 tcpdf 尚不支持输出图片。改起来也太费劲了
      

  25.   

    截屏$im = imagegrabscreen();
    imagepng($im, "myscreenshot.png");
    imagedestroy($im);不过很慢,1440 x 900 大约 十七八秒由于 php5.4 不再支持 com 了,imagegrabwindow 未作测试真不如 [Prtsc Sys Rq] 和 [Alt]+[Prtsc Sys Rq] 在粘贴到“画图”中来的快
      

  26.   

    看了不少资料,感觉php做这个目前确实很薄弱
      

  27.   

    如果你是在 linux 下,那么你应该可以找到借助浏览器内核做分析的 php 项目
      

  28.   

    啥意思?html2img?我用c#做过  就是访问一个网址 生成图片 
      

  29.   

    那就可以用webscreencapture.exe之类的PHP截屏软件。
      

  30.   

    可否把你的版本发出来测试一下,用的0.5.2这个版本的测试了,没效果,图片没显示,也没发现有xml支持的gd_adapter.cls.php 的751行屏蔽header("Content-type: image/png");這句就報錯了
      

  31.   

    我測試了一下0.6版,就算排除了所有的錯誤之後對於html的解析還是非常不理想
      

  32.   

    是的,是很不理想,php这个是个软肋啊
      

  33.   

    不满意可以改,不想改就自己写
    只要对 css 很熟悉的话,做起来并不复杂
    用 DOMDocument::loadHTML 载入 html 
    析出各个元素的属性
    计算每个盒子的尺寸和位置 这个比较复杂,需对 css 很熟悉
    逐一绘制
      

  34.   

    这个是什么意思?CSS,我自认还行,你说的用那个可以做到?还是什么?
    反正那个我测试了不行
      

  35.   


    http://www.tqcp.net/kong_bct.php
    你看他这个是如何做到的,他这个就是动态生成图片的哦,格式还很漂亮,究竟PHP怎么做到的?
      

  36.   

    给你个示例代码<?php
    $im = imagecreatefromstring(get());
    $ind = vertical($im);//取得垂直分布,用人工分析比较简单
    $w = imagesx($im);//抽取头部
    $top_h = 59;
    $top = imagecreate($w, $top_h);
    imagecopy($top, $im, 0, 0, 0, 0, $w, $top_h); 
    for($y=6; $y<29; $y++) imagecopy($top, $top, 0, $y, 0, 5, $w, 1);
    //imagegif($top); exit; //抽取中段(空白的)
    $middle_h = 29;
    $middle = imagecreate($w, $middle_h);
    imagecopy($middle, $im, 0, 0, 0, 59, $w, $middle_h);
    for($y=2; $y<$middle_h-1; $y++) imagecopy($middle, $middle, 0, $y, 0, 1, $w, 1);
    //imagegif($middle); exit;//抽取中段(红色分隔线空白的)
    $middlered_h = 29;
    $middlered = imagecreate($w, $middlered_h);
    imagecopy($middlered, $im, 0, 0, 0, 175, $w, $middle_h);
    for($y=2; $y<$middle_h-1; $y++) imagecopy($middlered, $middlered, 0, $y, 0, 1, $w, 1);
    //imagegif($middlered); exit;//抽取底部
    $bottom_h = 41;
    $bottom = imagecreate($w, $bottom_h);
    imagecopy($bottom, $im, 0, 0, 0, 318, $w, $bottom_h); 
    //imagegif($bottom); /*
    至此,我们已经准备好了组装空白彩票的部件。对于你自己的图片,当然是要自己动手画一些了
    他的组装规则是:
     头部 + ((行数 - 1) % 4) * 中段 + ceil((行数 - 1) / 4) * (红线中段 + 3 * 中段)+ 中段 + 底部(底部上压一线)现在就按次规则组装
    */
    $row = 11;
    $h = $top_h + (($row - 1) % 4) * $middle_h + ceil($row / 4) * 4 * $middle_h + $middle_h - 1 + $bottom_h;
    $new = imagecreate($w, $h);
    $y = 0;
    $k = 0;
    imagecopy($new, $top, 0, $y, 0, 0, $w, $top_h);
    $y += $top_h;
    for($i=0; $i<($row - 1) % 4; $i++, $y+=$middle_h) {
      imagecopy($new, $middle, 0, $y, 0, 0, $w, $middle_h);
      $k++;
    }
    for($i=0; $i<ceil($row / 4) * 4; $i++, $y+=$middle_h) {
      if($k++ && $i % 4 == 0) imagecopy($new, $middlered, 0, $y, 0, 0, $w, $middle_h);
      else imagecopy($new, $middle, 0, $y, 0, 0, $w, $middle_h);
    }
    imagecopy($new, $middle, 0, $y, 0, 0, $w, $middle_h);
    $y += $middle_h;
    imagecopy($new, $bottom, 0, $y-1, 0, 0, $w, $bottom_h);imagegif($new);function vertical($im) {
      $num = 0;
      for($y=0; $y<imagesy($im); $y++) {
        $p = imagecolorat($im, 6, $y);
        if($y == 0) $last = $p;
        if($p != $last && $num) {
          $res[$y-$num] = array($last, $num);
          $last = $p;
          $num = 0;
        }
        $num++;
      }
      $res[$y-$num] = array($last, $num);
      //print_r($res);//$ind 保存了像素的垂直分布
      return $res;
    }function get($n=5) {
      $url = 'http://www.tqcp.net/ct_end.php';
      $param = array('nqis' => $n, 'cmdOk' => '确定');
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      $r = curl_exec($ch);
      curl_close($ch);
      return $r;
    }
      

  37.   

    这个应该还是不一样的吧,你这个是抓取了他的页面,而他这个正好的是一个图片不过我想,他那个应该是自己用html组装成然后生成动态的吧,自己组装的这个我怎么搞都有瑕疵
      

  38.   


    刚刚想明白他的,应该是实现有了一张空白图片,然后利用这个空白图片重新组合的,不过他这个也有点限制了
    ,假设,我要弄成一行并列4个像他这样的一张图片,貌似就不好做的了,难道要不断判断么?要选择哪个模板图片么?就想有没有方便点,直接利用html组合的
      

  39.   

    要多少你就复制多少,事是死的人是活的dompdf 不满你的意,还有 html2ps、html2image
      

  40.   


    刚刚想明白他的,应该是实现有了一张空白图片,然后利用这个空白图片重新组合的,不过他这个也有点限制了
    ,假设,我要弄成一行并列4个像他这样的一张图片,貌似就不好做的了,难道要不断判断么?要选择哪个模板图片么?就想有没有方便点,直接利用html组合的
    你如果做过图片联系表(photo contactsheet),就知道了
    就是先定好行列数,每个thumb的尺寸,那么做底的图片尺寸也出来了
    然后对每张图做thumb,贴到底图的相应坐标位置就完成了
      

  41.   


    你好,麻烦问下, 我转文字,加了样式都没有问题,但是当包含img标签的时候,即我想将图文混排的内容变成图片,这个时候,那个img标签就不显示图片了。 只显示一个默认的X图片  要如何解决呢