Mr.T.K
Mr.T.K

Reputation: 2356

Convert From PHP and PDF

I need to convert from Php(html Table) to Pdf. I have used the DOM PDf for the conversion. But i didn't get the proper view in pdf. please find the below images.

Pdf View

PdfView

HTML View

Html View

Code Below:

<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  </head>
<?php if(count($this->userDetails)>=1){ for($a=0;$a<count($this->userDetails);$a++){ $user = $this->userDetails[$a]; ?>
  <body>
    <div style="background-image: url(http://192.168.2.26/skins/schooladmin/images/id_bg.png); background-repeat: repeat; width:400px; height:220px;margin:10px 0px 0px 10px;float:left;position:relative;">
<table style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; color:#fff; background-image:url(http://192.168.2.26/skins/schooladmin/images/id_white_bg.png); background-repeat:no-repeat;" width="100%" border="0" cellpadding="5" cellspacing="5">
  <tr>
    <td style="background-image: url(http://192.168.2.26/skins/schooladmin/images/schoollogo.png); background-repeat:no-repeat; width:36px; height:61px;">&nbsp;</td>
    <td colspan="2"><?php echo $this->schoolDetails['name'] ?></td>
    </tr>
<?php
    if($user){
    $user['userdetails']['name'] = $user['userdetails']['firstname'].$user['userdetails']['lastname'];
    $photoPath = BASE_PATH."/uploads/photos/student/".$user['userdetails']['photo'];
    if(!file_exists($photoPath)){
        $photoPath = BASE_PATH."/skins/schooladmin/images/id_photo.png";
    }
    }else{
        $photoPath = BASE_PATH."/skins/schooladmin/images/id_photo.png";
    }
?>
  <tr>
    <td style="background-image: url(<?php echo $photoPath; ?>); background-repeat:no-repeat; width:82px; height:82px;">&nbsp;</td>
    <td colspan="2">
    <table width="100%" border="0" cellspacing="3" cellpadding="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;" >
    <?php if($this->templateDetails['template']){ 
        for($i=0;$i<1;$i++){
            for($j=0;$j<count($this->templateDetails['template_details']);$j++){
                if($this->templateDetails['template_details'][$j]['type'] == "text" && $this->templateDetails['template_details'][$j]['page'] == "front"){
    ?>
      <tr>
        <td width="38%"><?php echo $this->templateDetails['template_details'][$j]['label']; ?></td>
        <td width="62%"><?php echo @$user['userdetails'][$this->templateDetails['template_details'][$j]['field']]; ?></td>
      </tr>
    <?php } } } } ?>
    </table>
    </td>
    </tr>
    <tr>
        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;"  align="center"><?php echo $this->schoolDetails['address'] ?>, <?php echo $this->schoolDetails['address2'] ?>, <?php echo $this->schoolDetails['city'] ?> - <?php echo $this->schoolDetails['zipcode'] ?></td>
    </tr>
    <tr>
        <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;"  align="center">Ph: <?php echo $this->schoolDetails['contactno'] ?></td>
    </tr>
</table>
    </div>

    <div style="background-image: url(http://192.168.2.26/skins/schooladmin/images/id_bg.png); background-repeat: repeat; width:400px; height:220px;margin:10px 0px 0px 10px;float:left;position:relative;">
<table style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; color:#fff; background-image:url(http://192.168.2.26/skins/schooladmin/images/id_white_bg.png); background-repeat:no-repeat;" width="100%" border="0" cellpadding="5" cellspacing="5">
    <?php if($this->templateDetails['template']){ 
        for($i=0;$i<1;$i++){
            for($j=0;$j<count($this->templateDetails['template_details']);$j++){
                if($this->templateDetails['template_details'][$j]['type'] == "text" && $this->templateDetails['template_details'][$j]['page'] == "back"){
    ?>
      <tr>
        <td width="38%"><?php echo $this->templateDetails['template_details'][$j]['label']; ?></td>
        <td width="62%"><?php echo @$user['userdetails'][$this->templateDetails['template_details'][$j]['field']]; ?></td>
      </tr>
    <?php } } } } ?>
</table>
    </div>
    <?php } }  ?>
  </body>
</html>

Upvotes: 0

Views: 1917

Answers (2)

ghoti
ghoti

Reputation: 46846

I recommend not trying to massage your HTML so that it converts to PDF this way. You'll expend far to much effort tweaking things that can change when you switch rendering engines or even printers.

You'll have an easier time if you create your PDFs natively using FPDF or TCPDF. For example (just to get you started):

<?php

require_once('/path/to/tcpdf.php');

// misc variables
$lightblue = array(100, 100, 250);
$darkblue = array(0, 0, 255);
$white = array(255, 255, 255);

// prepare new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetFont('helvetica', 'B', 20);

// add a page
$pdf->AddPage();

// set the coordinates of the gradient
$coords = array(0, 1, 1, 0);

// paint a linear gradient
$pdf->LinearGradient(20, 45, 105, 60, $lightblue, $darkblue, $coords);

// write text
$pdf->SetTextColorArray($white);
$pdf->Text(40, 50, 'hello');
$pdf->Text(40, 60, 'world');

// Close and output PDF document
$pdf->Output('tkexample.pdf', 'I');

Upvotes: 1

Kaidul
Kaidul

Reputation: 15875

For geeting 100% same view, use php fpdf library.You can get all the supports, tutorials and download from here

Upvotes: 0

Related Questions