TCPDF在mysql

如何解决TCPDF在mysql

我需要帮助。

我正在尝试从数据库准备pdf,但结果是空白。 pdf是表格格式,具有单独的内联样式。表格设计具有非常复杂的条件,例如根据客户要求根据某些条件进行行和列合并。

这是我在测试中发现的:

  1. 从数据库中获取少量记录-pdf完美显示
  2. 从数据库中提取的大量记录-pdf显示为空白(屏幕上没有其他错误,只有纯白色的pdf,并且在编码中设置了页眉和页脚)
  3. 如果有大量记录,则仅以HTML回显-所有记录均以表格格式完美显示。

我做了什么: 增加了时间限制,执行时间,使用了get_ob_contents和所有ob系列以及我在Google指导下在stackflow中找到的大多数解决方案,但是我没有找到解决方案。

我将所有sql结果存储在一个数组中,因为我必须根据某些字段进行排序,该字段具有来自另一个表的排序顺序(在sql中很多左联接)。而不是添加到左连接,我想到了使用带有选定字段的array_multisort。 pdf是基于sql查询生成的,该sql查询具有多个左连接和以html形式使用设置的where子句。一旦array_multisort仅完成,则将for循环设置为使用writeHTML()展开变量以供pdf执行

测试示例:

  1. sql结果较少(估计:在测试期间为20条记录)的pdf结果可完美生成分页符和页码。

  2. 带有多条记录(149)pdf的sql结果,出现纯白色单页且仅页眉和页脚

我似乎找不到解决方案以表格式显示大量sql数据的pdf。

是否有更好的方法来实现这一目标?

顺便说一句,我使用的是TCPDF版本6.2.9,这让我头疼不已,所以我下载了6.3.1,结果相同。

    <?php
error_reporting(E_ALL);
ini_set('display_errors',"On");

//require_once('tcpdf_include.php');
$original_mem=ini_get('memory_limit');
ini_set('memory_limit','340M');
session_start();
ob_start();

// Include the main TCPDF library (search for installation path). 
include '../../dbc.php';
include '../../func.php';

function conditional_format($data){
    //function for conditional formatting
    //with table row structure 
return $formatted_data;
}

require_once('tcpdf_include.php');
// Increase max_execution_time. If a large pdf fails,increase it even more.
ini_set('max_execution_time',180);
// Increase this for old PHP versions (like 5.3.3). If a large pdf fails,increase it even more.
ini_set('pcre.backtrack_limit',1000000);
    




//below function to set footer align in middl of page
class MYPDF extends TCPDF {
    public function Header() {

        $curdate=date("d/m/Y");
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->AutoPageBreak;
        // disable auto-page-break
        $this->SetAutoPageBreak(false,0);
        if($this->page==1){
            //client requested title only on first page
            $this->Cell(0,9,'TRADEMARK STATUS REPORT','B',false,'C','','M','M');
        }
        $this->SetFont('helvetica',10);
        $this->SetAutoPageBreak($auto_page_break,$bMargin);
        $this->setPageMark();
    }

    public function Footer() {
        $curdate=date("d/m/Y");
            // Position at 10 mm from bottom
        $this->SetY(-10);
        // Set font
        $this->SetFont('helvetica','I',8);
        // Page number
        $this->Cell(0,10,'as at '.$curdate,'T','L','M');
        $this->Cell(0,'Page '.$this->getAliasNumPage(),'R','M');
    
    }
}

//call custom page foooter from function above
$pdf=new MYPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Ryan');
$pdf->SetTitle('Trade Mark Status Report');
$pdf->SetSubject('Trade Mark');
$pdf->SetKeywords('Trade Mark,PDF,example,test,guide');

// 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 (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
   $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('freeserif',20);

// add a page
$pdf->setHeaderFont(array('',14));
$pdf->AddPage('L','A4');

$pdf->SetFont('freeserif',8);

$main_sql="SELECT ..... WHERE 
$user_emtered_where_clause";

//echo $main_sql;
// there is no issue with sql when echo

$result=mysqli_query($db,$main_sql);
$tbl="";
if(!$result){
    echo "Error: ".mysqli_error($db);
}else{
    if(mysqli_num_rows($result)>0){
        while($main_rs=mysqli_fetch_array($result)){
            //get record data from DB
            // design tbl header with selected record 
            $tbl = <<<EOF
            
            <table width="950" v-align="top" style="border-collapse: collapse;font-size:13px;table-layout:fixed;" border="0" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <td width="950" colspan="7" style="padding:3px;text-align:center;background-color:lightgray;border-bottom:1px solid black;"><h2 style="font-weight:bold;line-height:20px;">$ProjectCompany</h2>
                        </td>
                    </tr>
                    <tr>
                        <td width="950"  colspan="7" style="font-size:17px;font-weight:bold;vertical-align:top;"><br>
                            $TMClient
                        </td>                       
                    </tr>
                    <tr>
                        <td width="30" style="text-align:center;border-left:1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">No</td>
                        <td width="100" style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Filing Country</td>
                        <td width="175" style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Applicant/Proprietor</td>
                        <td width="100" style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Trademark No</td> 
                        <td width="205" style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Trademark</td>
                        <td width="80"  style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Status</td>
                        <td width="260" style="text-align:center;border-right: 1px solid black;border-bottom: 1px solid black;border-top:1px solid black;background-color: azure;font-weight:bold;">Remarks</td>                                    
                    </tr>
                </thead>
            <tbody>             
    EOF;
        
        //assign data to array           
      
        $data_array[]=array(all record data here);// assign to array
            
        
    }
    
    //sort array
    foreach ($data_array as $key => $row){
        $sort_country[$key] = $row['sort'];
        //to get the sort order value from array
    }
    // rearrange the array based on the sort order value above
    array_multisort($sort_country,SORT_ASC,$data_array);
    //view array data
     //echo "<pre>";
     //print_r($data_array);
     //echo "</pre>":
     // array data all appear perfect.
    
    if(count($data_array)>0){
        
        $n=1;
        // construct table body data
        foreach($data_array as $item){
            set_time_limit(60);
            
            $no =$n;
            //------------------------ construction of table body -----------------------------------
            
                $class_data= conditional_format($classid); // call conditional format function with variable from $data_array    
                
            $pbreak=$conditional_page_break;
            $tbl_data .= <<<EOD
            $pbreak  

                    <tr nobr="true">
                        <td width="950"  padding="0" style="text-align:left;"><table width="950" style="font-size:13px;table-layout:fixed;border:1px solid red;" border="1" cellpadding="2" cellspacing="0">                                
                                
                                <tr nobr="true">
                                    <td width="30" height="105" style="text-align:center;vertical-align:top;border-top:1px solid black;border-left:1px solid black;border-right: 1px solid black;">$no</td>
                                    <td width="100" height="105" v-align="top" style="text-align:center;vertical-align:top;border-top:1px solid black;border-right: 1px solid black;border-bottom:1px solid black;">$TMCountry</td>
                                    <td width="175" height="105" v-align="top" style="text-align:left;border-top:1px solid black;border-right: 1px solid black;border-bottom:1px solid black;">$TMEffecOwner</td>
                                    <td width="100" height="105" v-align="top" style="vertical-align:top;border-top:1px solid black;text-align:center;border-right: 1px solid black;border-bottom:1px solid black;">$TMApplnNo</td> 
                                    <td width="205" height="105" style="text-align:center;word-wrap: break-word;border-top:1px solid black;border-right: 1px solid black;border-bottom:1px solid black;"><br><br>$logo $imgType</td>
                                    <td width="80" height="105" v-align="top" style="border-top:1px solid black;text-align:center;vertical-align:top;border-right: 1px solid black;border-bottom:1px solid black;">$Status</td>
                                    <td width="260" height="105" v-align="top" style="border-top:1px solid black;vertical-align:top;border-right:1px solid black;border-bottom:1px solid black;">$Remarks</td>
                                </tr>
                            </table>
                        </td>
                    </tr>                                                
                    <tr nobr="true">
                        <td width="950" align="center" style="text-align: center; padding:0px;"><table width="950" v-align="top" style="margin-left: auto;margin-right: auto; font-size:13px;padding:3px;" border="0" cellpadding="0" cellspacing="0" >
                                <tr>
                                    <td width="30" style="border-left:1px solid black;border-right:1px solid black;">&nbsp;&nbsp;</td>
                                    <td width="100" style="border-left:1px solid black;font-weight:bold;background-color:#F3F3F3;">Class: &nbsp;&nbsp;</td>
                                    <td width="820" style="border-right:1px solid black;font-weight:bold;background-color:#F3F3F3;">Specification: &nbsp;&nbsp;</td>
                                </tr>
                                $class_data
                            </table>
                        </td>                                                    
                    </tr> 
                        
                              
        EOD;
            $n++;
            $linecount++;
            }
        
        }else{
            $tbl= "no record";
        }
    }   
    $tbl.=$tbl_data."
                            </tbody> 
                        </table>
                        ";
$tbl_decode=ob_get_contents();
$pdf->SetY(10);
ob_end_clean();
//echo $tbl_decode; //this echo does produce HTML version of entire table
$pdf->writeHTML($tbl_decode,''); // this is where I get blank pdf page
$pdf->Output('Trademark_Status_Report_with_spec.pdf','I');

    ini_set('memory_limit',$original_mem);

?>

解决方法

我找到了原因。

首先..其时限问题 所以我将时间限制更改为以下

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="hello_world.MainPage">
    <StackLayout>
        <Label 
            Text="Welome" 
            BackgroundColor="Yellow" 
            TextColor="Green" 
            HeightRequest="{ConstraintExpression Type=RelativeToView,Factor=0.05,Constant=0}"
            FontSize="Medium" />
    </StackLayout>

</ContentPage>

第二个问题与sql中的文本字段有关

No property,BindableProperty,or event found for "HeightRequest",or mismatching type between value and property.

现在解决了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res