折雨的天空

新浪微薄腾讯微薄

最新碎语:最近霉到了住。哎。。。

您的位置:折雨的天空 >php开发> 发一下优乐笑话生成PDF的代码吧

发一下优乐笑话生成PDF的代码吧

使用的是开源类TCPDF来生成的, 这个类主要是写中文的时候比较容易,另外写页头,页尾和目录,书签这些常用的

功能在他官方的demo里都能找到代码,遗憾的是文档较少,中文资料也较少,我根据网上的一个资料制作了一个支持

中文的字体,并根据他的demo增加改写了一些,最终勉强实现了我想要的效果,以下是PHP代码:

<?php
/**
 * @author 我好笨
 * @todo 生成PDF测试
 * 
 */
 set_time_limit(0);
 require_once "./config/comm_config.php";
 require_once __ROOTDIR__."/include/comm.php";
 require_once __ROOTDIR__."/include/tcpdf/config/tcpdf_config.php";
 require_once(__ROOTDIR__.'/include/tcpdf/config/lang/eng.php');
 require_once(__ROOTDIR__.'/include/tcpdf/tcpdf.php');
 $number_pdf=_get("curent");
 $number_pdf=$number_pdf?intval($number_pdf):0; 
 //取总记录数
 $nums=$db->getNums("select * from `{$db_prefix}article_base` where `{$db_prefix}article_base`.`is_check`=1");
 if($number_pdf<ceil($nums/365))
 {
    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('优乐笑话网');
    $pdf->SetTitle('优乐笑话365开心每一天第'.($number_pdf+1).'部');
    $pdf->SetSubject('优乐笑话365开心每一天第'.($number_pdf+1).'部');
    $pdf->SetKeywords('优乐笑话,365开心每一天,第'.($number_pdf+1).'部');
    
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, '优乐笑话365开心每一天第'.($number_pdf+1).'部', PDF_HEADER_STRING);
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    //set some language-dependent strings
    $pdf->setLanguageArray($l);
    
    // ---------------------------------------------------------
    
    // set font
    $pdf->SetFont('dsf', '', 10);
    
    // add a page
    $pdf->AddPage();
    
    //查询数据库,取最近20条记录
    $table_article_base=$db->as_field("{$db_prefix}article_base");
    $table_article=$db->as_field("{$db_prefix}article","c");
    $table_sort=$db->as_field("{$db_prefix}sort","s");
    $sql="select $table_article_base,$table_article,$table_sort from `{$db_prefix}article_base` left join `{$db_prefix}article` on `{$db_prefix}article_base`.`id`=`{$db_prefix}article`.`article_id` left join `{$db_prefix}sort` on `{$db_prefix}article_base`.`sort`=`{$db_prefix}sort`.`id` where `{$db_prefix}article_base`.`is_check`=1 limit ".($number_pdf*365).",365";
    $re=$db->query($sql);
    $article=array();
    $txt = '';
    while($rd=$db->fetchArray($re))
    {
        $article=$rd;
        $article['tags']=explode(",",$rd['tag']);
        $article['sys_time']=$rd['add_time'];
        $article['add_time']=$rd['add_time']?date("Y-m-d",$rd['add_time']):"";
        $article['ding']=intval($rd['do']);
        $article['cai']=intval($rd['undo']);
        $article['title']=$rd['title'];
        $article['info']=htmlspecialchars($rd['info']);
        $article['content']=str_replace($str_replace,$str_replace_r,str_replace("#p#分页标题#e#","",$rd['c_content']));
        $pdf->setDestination($article['title'], 0, '');
        $pdf->Bookmark($article['title'], 0, 0, '', 'B', array(0,64,128));
        $pdf->SetFont('dsf', 'B', 16);
        $pdf->writeHTML($article['title']."<br />");
        $pdf->SetFont('dsf', '', 10);
        $ec=preg_replace_callback("~(?=[\/]*index.php\?ct=)(.*)(?=[\"\'])~Uis",create_function('$matchs','return "http://www.meiyoule.com"._parse_html($matchs[0]);'),$article['content']);
        $ec=preg_replace("~(\./|/)?index\.php~Uis","http://www.meiyoule.com/index.html",$ec);
        $pdf->writeHTML(strip_tags($ec,"<br /><a><p>")."<br />");
        $pdf->writeHTML("优乐笑话原文连接:<a href=''>http://www.meiyoule.com/view/index/id-".$rd['id'].".html</a><br />");
    }
    
    // add a new page for TOC
    $pdf->addTOCPage();
    
    // write the TOC title
    $pdf->SetFont('dsf', 'B', 16);
    $pdf->MultiCell(0, 0, '本期内容索引', 0, 'C', 0, 1, '', '', true, 0);
    $pdf->Ln();
    
    $pdf->SetFont('dsf', '', 12);
    
    // add a simple Table Of Content at first page
    // (check the example n. 59 for the HTML version)
    $pdf->addTOC(1, 'dsf', '.', '目录', 'B', array(128,0,0));
    
    // end of TOC page
    $pdf->endTOCPage();
    
    //Close and output PDF document
    $pdf->Output('meiyou'.($number_pdf+1).'.pdf', 'F');
    $url=array("0"=>"pdf.php?curent=".($number_pdf+1),"继续生成"=>"pdf.php?curent=".($number_pdf+1));
    $error_msg='文件meiyou'.($number_pdf+1).'.pdf生成完成,继续生成后续文件';
    default_err($url,$error_msg,0,3);
 }
 else
 {
    //完成
    $url=array("首页"=>"index.php");
    $error_msg='所有文件已生成完成';
    default_err($url,$error_msg,0);
 }

------------正 文 已 结 束, 感 谢 您 的 阅 读 (折雨的天空)--------------------

转载请注明本文标题和链接:《发一下优乐笑话生成PDF的代码吧

奖励一下

取消

分享不易,烦请有多多打赏,如您也困难,点击右边关闭即可!

扫码支持
扫码打赏,5元,10元,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

-秒后自动关闭,如已打赏,或者不愿打赏,请点击右上角关闭图标。

发表评论

路人甲 表情
看不清楚?点图切换