douyao4632 2010-06-06 23:26
浏览 26
已采纳

Zend Framework PDF示例为excel类型

How can I make something like this with Zend_PDF:

I have a few columns A, B, C, D, E and after them I will have some information, something like excel.

A | B | C | D

ss|das|dad|ds

ss|das|dad|ds

ss|das|dad|ds

How can I create something like this with ZF_pdf? I will pull the data from DB.

  • 写回答

1条回答 默认 最新

  • drs3925 2010-06-07 12:17
    关注

    This is a very simple working example that loads data from a file and creates a PDF from that:

    require_once 'Zend/Pdf.php';
    
    $pdf = new Zend_Pdf();
    $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    
    define('FONT_SIZE'      , 12);
    define('MARGIN_LEFT'    , 40);
    define('MARGIN_TOP'     , 40);
    define('COL_WIDTH'      , 100);
    define('COL_LEFT_MARGIN', 10);
    define('COL_SEPARATOR'  , '|');
    define('ROW_HEIGHT'     , 20);
    
    $row = 1;
    $x = MARGIN_LEFT;
    $y = MARGIN_TOP;
    
    $page->setFont($font, FONT_SIZE);
    
    if (($handle = fopen('data.csv', 'r')) !== false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== false) {
            $num = count($data);
            $row ++;
            for ($i = 0; $i < $num; $i++) {
                $page->drawText($data[$i], $x, $page->getHeight() - $y);
                $x += COL_WIDTH;
                $page->drawText(COL_SEPARATOR, $x, $page->getHeight() - $y);
                $x += COL_LEFT_MARGIN;
            }
            $x = MARGIN_LEFT;
            $y += ROW_HEIGHT;
        }
        fclose($handle);
    }
    $pdf->pages[] = $page;
    $pdf->save('data.pdf', true);
    

    Where the CSV file contains arbitrary data, e.g.:

    "A","B","C","D"
    "asd","daasd","sdfs","dfsdf"
    "asd","daasd","sdfs","dfsdf"
    "asd","daasd","sdfs","dfsdf"
    

    Of course, the data source could be anything else. Note that this example is not able to deal with large strings, multiple pages, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度