【问题标题】:to convert php report page to PDF format将php报告页面转换为PDF格式
【发布时间】:2016-04-05 17:18:16
【问题描述】:

我是 php 新手,不知道如何将 php 文件转换为 pdf。

我正在尝试将以下代码转换为 pdf,但我没有成功。

请帮我转换一下,这是一种紧急情况。我还附上了一张我想要转换的图片。

<?
$Section = isset($_GET['Section'])?$_GET['Section']:"";
?>


<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <div id="loginpanel" >
    <h3>Performa regarding Contempt Petition ('A'Category) Cases Report</h3>
  </div> 
<br />
<p id="demo" align="right" style="font-size:12px"></p>

<a href="<?=DIR_WS_SITE_CONTROL?>index.php?Page=pdf"          target="_blank">Download PDF</a>

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
</script>

<tr><td><br/></td></tr>
</table>

<?
  //// Section start
switch($Section)
{
   case "district_report":
   default:


 $ZoneObj = new DataTable(TABLE_ZONE);
 $ZoneObj->Where = "1";
 $ZoneObj->TableSelectAll();

 ?>

<table cellpadding="5" cellspacing="1" border="1" class="TableBorder"    style="color:black">

   <tr class="InsideLeftTd">
      <td width="2%" height="25"><b>S.No</b></td>
      <td width="10%" align="left"><b>Circle</b></td>
      <td width="10%" align="left"><b>District</b></td>
      <td width="8%" align="left"><b>Total no of cases</b></td>
      <td width="8%" align="left"><b>Total no. of cases where reply filed</b></td>
      <td width="8%" align="left"><b>No. of cases due for hearing in next 21 days</b></td>
      <td width="8%" align="left"><b>No. of cases due for hearing in next 21 days(Reply Filed)</b></td>
      <td width="8%" align="left"><b>No. of cases due for hearing in next 21 days(Reply Not Filed)</b></td>
  </tr>
 <?

 $SNo=1;
 $Count=1;
 while($CurrentZon=$ZoneObj->GetObjectFromRecord())
 {
   $circleObj = new DataTable(TABLE_CIRCLE);
   $circleObj->Where = "zone_id='".$CurrentZon->id."'";
   $circleObj->TableSelectAll();

 ?>
<tr>
  <td colspan="11" align="center"><b><?=MyStripSlashes($CurrentZon->zone);?> Zone</b></td>
</tr>


<?  while($CurrentCir=$circleObj->GetObjectFromRecord())
 {  

   $DistrictObj = new DataTable(TABLE_DISTRICT);
   $DistrictObj->Where = "circle_id='".$CurrentCir->id."'";
   $DistrictObj->TableSelectAll();

 ?>

 <tr class="InsideRightTd">

    <td height="25" align="center" style="color:black" rowspan="<?=$DistrictObj->GetNumRows()?>"><?=$SNo?>.</td>

    <td align="left" style="color:black" rowspan="<?=$DistrictObj->GetNumRows()?>">
        <?=MyStripSlashes($CurrentCir->circle);?>
    </td>

  <?    while($CurrentDis=$DistrictObj->GetObjectFromRecord())
  {

   $ConObj = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj->Where = "district_id='".$CurrentDis->id."'";
   $Case_count=$ConObj->TableCountDistinct("id","id_count");

   $ConObj2 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj2->Where = "district_id='".$CurrentDis->id."' AND reply_filed='Yes'";
   $Reply_count=$ConObj2->TableCountDistinct("id","reply_count");

   $ContObj = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ContObj->Where = "district_id='".$CurrentDis->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."'";
   $ContObj->TableSelectAll();

   $ConObj3 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj3->Where = "district_id='".$CurrentDis->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='Yes'";
   $ConObj3->TableSelectAll();

   $ConObj4 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj4->Where = "district_id='".$CurrentDis->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='No'";
   $Filed_count=$ConObj4->TableCountDistinct("id","next_count");

   ?>
    <td align="center" style="color:black"><?=MyStripSlashes($CurrentDis->district);?></td>
    <td align="center" style="color:black"><?=MyStripSlashes($Case_count->id_count);?></td>

   <?   if($Reply_count->reply_count!='0'){
        $color= (($Reply_count->reply_count)*100)/$Case_count->id_count;
    }else{
        $color = '0';
    }

    if($color=='0'){?>
      <td align="center" style="color:black">
        <?=MyStripSlashes($Reply_count->reply_count)?>
      </td>

    <?}elseif($color<=65 & $color>0){?>

    <td align="center" style="color:black" bgcolor="#F75D59">
        <?=MyStripSlashes($Reply_count->reply_count)?>
    </td>

    <? }elseif($color>65 && $color<=90){?>
    <td align="center" style="color:black" bgcolor="#FFFF00">
        <?=MyStripSlashes($Reply_count->reply_count)?>
    </td>

    <? }elseif($color>90){?>
    <td align="center" style="color:black" bgcolor="#00FF00">
        <?=MyStripSlashes($Reply_count->reply_count)?>
    </td>           
    <?}?>

    <td align="center" style="color:black">
        <?=MyStripSlashes($ContObj->GetNumRows())?>
    </td>

    <td align="center" style="color:black">
        <?=MyStripSlashes($ConObj3->GetNumRows())?>
    </td>


     <? if($Filed_count->next_count==0){?>

      <td align="center" style="color:black" bgcolor="#00FF00">
        <?=MyStripSlashes($Filed_count->next_count)?>
       </td>
      <?}else{?>
       <td align="center" style="color:black" bgcolor="#F75D59">
        <?=MyStripSlashes($Filed_count->next_count)?>
        </td>
        <?}?>

     </tr>


     <? 
   } 
    $SNo++;
    $Count++;

   ////////Circle Total Starts here////////////////////////////

   $ConObj5 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj5->Where = "circle_id='".$CurrentCir->id."'";
   $Case_count=$ConObj5->TableCountDistinct("id","id_count");

   $ConObj6 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj6->Where = "circle_id='".$CurrentCir->id."' AND reply_filed='Yes'";
   $Reply_count=$ConObj6->TableCountDistinct("id","reply_count");

   $ContObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ContObj7->Where = "circle_id='".$CurrentCir->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."'";
   $Hear_count=$ContObj7->TableCountDistinct("id","file_count");

   $ConObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj7->Where = "circle_id='".$CurrentCir->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='Yes'";
   $Hearing_count=$ConObj7->TableCountDistinct("id","case_count");

   $ConObj8 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj8->Where = "circle_id='".$CurrentCir->id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='No'";
   $Filed_count=$ConObj8->TableCountDistinct("id","next_count");       

   ?>
    <tr>
      <td colspan="3" align="center"><b><?=$CurrentCir->circle?> Circle Total</b></td>
    <td align="center" style="color:black"><b><?=MyStripSlashes($Case_count->id_count);?></b></td>

     <? if($Reply_count->reply_count!='0'){
        $color= (($Reply_count->reply_count)*100)/$Case_count->id_count;
      }else{
        $color = '0';
       }

       if($color=='0'){?>
      <td align="center" style="color:black">
        <?=MyStripSlashes($Reply_count->reply_count)?>
      </td>

    <?}elseif($color<=65 & $color>0){?>

      <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
      </td>

    <? }elseif($color>65 && $color<=90){?>
    <td align="center" style="color:black" bgcolor="#FFFF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
    </td>

    <? }elseif($color>90){?>
    <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
    </td>           
    <?}?>

    <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hear_count->file_count)?></b>
    </td>

    <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hearing_count->case_count)?></b>
    </td>

    <?  if($Filed_count->next_count==0){?>

    <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}else{?>
    <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}?>

   </tr>

   <?
   /////////Zone total starts here/////////////////////////

   $ConObj5 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj5->Where = "zone_id='".$CurrentCir->zone_id."'";
   $Case_count=$ConObj5->TableCountDistinct("id","id_count");

   $ConObj6 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj6->Where = "zone_id='".$CurrentCir->zone_id."' AND reply_filed='Yes'";
   $Reply_count=$ConObj6->TableCountDistinct("id","reply_count");

   $ContObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ContObj7->Where = "zone_id='".$CurrentCir->zone_id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."'";
   $Hear_count=$ContObj7->TableCountDistinct("id","fil_count");

   $ConObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj7->Where = "zone_id='".$CurrentCir->zone_id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='Yes'";
   $Hearing_count=$ConObj7->TableCountDistinct("id","case_count");

   $ConObj8 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj8->Where = "zone_id='".$CurrentCir->zone_id."' AND date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='No'";
   $Filed_count=$ConObj8->TableCountDistinct("id","next_count");    



    } ?>

    <tr>
    <td colspan="3" align="center"><b><?=$CurrentZon->zone?> Zone Total</b>      </td>

    <td align="center" style="color:black"><b><?=MyStripSlashes($Case_count->id_count);?></b></td>

  <?    if($Reply_count->reply_count!='0'){
        $color= (($Reply_count->reply_count)*100)/$Case_count->id_count;
      }else{
        $color = '0';
      }

     if($color=='0'){?>
      <td align="center" style="color:black">
        <?=MyStripSlashes($Reply_count->reply_count)?>
      </td>

      <?}elseif($color<=65 & $color>0){?>

      <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
      </td>

      <? }elseif($color>65 && $color<=90){?>
      <td align="center" style="color:black" bgcolor="#FFFF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
      </td>

      <? }elseif($color>90){?>
       <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
       </td>            
       <?}?>

        <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hear_count->fil_count)?></b>
        </td>

        <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hearing_count->case_count)?></b>
    </td>

   <?   if($Filed_count->next_count==0){?>

    <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}else{?>
    <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}?>

    </tr>


   <?}

   /////////District total starts here//////////////////////////

   $ConObj5 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj5->Where = "1";
   $Case_count=$ConObj5->TableCountDistinct("id","id_count");

   $ConObj6 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj6->Where = "reply_filed='Yes'";
   $Reply_count=$ConObj6->TableCountDistinct("id","reply_count");

   $ContObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ContObj7->Where = "date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."'";
   $Hear_count=$ContObj7->TableCountDistinct("id","file_count");

   $ConObj7 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj7->Where = "date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='Yes'";
   $Hearing_count=$ConObj7->TableCountDistinct("id","case_count");

   $ConObj8 = new DataTable(TABLE_CONTEMPT_PETITION_A);
   $ConObj8->Where = "date_next_hearing>='".date('Y-m-d')."'  AND date_next_hearing <='".date('Y-m-d', strtotime("+21 days"))."' AND reply_filed='No'";
   $Filed_count=$ConObj8->TableCountDistinct("id","next_count");    

   ?>    
    <tr class="InsideRightTd">
    <td align="center" style="color:black" bgcolor="#F5F5DC" colspan="3"><b> Grand Total</b></td>
    <td align="center" style="color:black" bgcolor="#F5F5DC"><b><?=MyStripSlashes($Case_count->id_count);?></b></td>

    <?  if($Reply_count->reply_count!='0'){
        $color= (($Reply_count->reply_count)*100)/$Case_count->id_count;
    }else{
        $color = '0';
    }

    if($color=='0'){?>
      <td align="center" style="color:black">
        <?=MyStripSlashes($Reply_count->reply_count)?>
      </td>

    <?}elseif($color<=65 & $color>0){?>

      <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
      </td>

    <? }elseif($color>65 && $color<=90){?>
    <td align="center" style="color:black" bgcolor="#FFFF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
    </td>

    <? }elseif($color>90){?>
    <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Reply_count->reply_count)?></b>
    </td>           
    <?}?>

    <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hear_count->file_count)?></b>
    </td>

    <td align="center" style="color:black">
        <b><?=MyStripSlashes($Hearing_count->case_count)?></b>
    </td>

    <?  if($Filed_count->next_count==0){?>

    <td align="center" style="color:black" bgcolor="#00FF00">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}else{?>
    <td align="center" style="color:black" bgcolor="#F75D59">
        <b><?=MyStripSlashes($Filed_count->next_count)?></b>
    </td>
    <?}?>
    </tr>

   </table>

  <?    break;
   }
  ?>

   <table cellpadding="0" cellspacing="0" border="0" style="color:black" align="center">
     <tr></tr><br/>

     <tr>
      <td>Less than 65% &nbsp;&nbsp;<div style="width:60px;height:20px;border:1px solid #000;background:#F75D59;"></div>     </td>
      <td>Between 65-90%&nbsp;&nbsp;<div style="width:60px;height:20px;border:1px solid #000;background:#FFFF00;"></div></td>
      <td>Greater Than 90% &nbsp;&nbsp;<div style="width:60px;height:20px;border:1px solid #000;background:#00FF00;"></div></td>
      </tr>

    </table>

这是一份报告,我希望下载它的 pdf。

我也附上它的图片

【问题讨论】:

  • 看看wkhtmltopdf 命令行工具。它允许将 html 渲染为 pdf。

标签: php pdf


【解决方案1】:

还有一个免费的windows软件叫做DoPdf。它就像您在 PC 上安装的虚拟打印机,非常轻巧、简单、快速且无痛。在您的 PC 上安装此“打印机”,然后在您的浏览器上,在该打印机上“打印”页面。您将得到的只是将漂亮而干净的 HTML 转换为保存在您选择的文件夹中的 PDF 文件。您甚至可以配置生成的 PDF 的分辨率,如果它是纵向或横向、黑白或彩色,那么,这是一个非常受欢迎和不错的选择。此外,您 PC 上的每个软件都可以随时随地使用这个 pdf 创建器,例如,您的 OpenOffice 等。最好的。

【讨论】:

  • 感谢您的回答,但问题是“需要使用不同的计算机从不同的位置下载此报告,而不仅仅是我的”
  • 那么您将需要依赖服务器端资源,就像(我认为)Alexander Popov 在我之前对您说的那样。不要害怕,实施起来并不难。 1) 联系您的 ISP(您的网站所在的地方); 2) 询问他们是否有满足这种需求的服务器端组件——“onfly”创建一个 PDF——以及你的帐户是否负担得起使用该技术; 3)向他们询问要在您的页面上实现的代码示例; 4) 是时候编写代码了:根据您的所有知识,在您的页面上放置一个“下载为 PDF”,供用户单击和管理后台。我相信你会明白的。最好的。
  • @Sonia 请检查您认为对解决您的问题有帮助的答案。谢谢。
  • 您好,您有其他解决方案来创建我网站的任何网页的 PDF 文件吗...
  • 你试过我写给你的食谱了吗?重点在哪里?如果您找到其他解决方案,请提醒我,因为这将是一个真实的发现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多