Part of the EllisLab Network
   
3 of 4
3
Plugin to generate PDFs on the fly
Posted: 02 February 2007 02:42 PM   [ Ignore ]   [ # 31 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

Anyone here got a viewfile that pulls information from the database and outputs the HTML to PDF?? If so, could you post your code?

I have a invoicepdf.php file which was parsed for the CI template but it didnt work so I added PHP code directly in the invoicepdf.php and getting the same error…
Parse error: syntax error, unexpected $end in /home/beemcom/public_html/system/application/views/invoices/invoicepdf.php on line 143

I can post my view file is anyone wants it

Profile
 
 
Posted: 02 February 2007 02:45 PM   [ Ignore ]   [ # 32 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

Here is my view file

<?
$id
= $this->uri->segment(3);
$query = $this->db->query("SELECT * FROM invoices WHERE invoiceno = ".$id."");
$invoice = $query->row();
$clientdet = $this->db->query("SELECT * FROM clients WHERE accno = ".$invoice->clientid."");
$client = $clientdet->row();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<
title>Invoice #<?= $this->uri->segment(3); ?></title>
<style type="text/css">
<!--
.
style1 {
    font
-family: Arial, Helvetica, sans-serif;
    
font-weight: bold;
}
.style2 {
    font
-size: 24px;
    
font-weight: bold;
    
font-family: Arial, Helvetica, sans-serif;
    
color: #FFFFFF;
}
.style4 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; }
.style5 {
    font
-size: 14px;
    
font-weight: bold;
    
font-family: Arial, Helvetica, sans-serif;
}
.style12 {color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; }
.style20 {font-family: Arial, Helvetica, sans-serif; font-size: 13px; }
.style22 {font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; }
-->
</
style>
</
head>

<
body>
<
div align="center">
  <
table width="100%" border="0" cellspacing="0" cellpadding="0">
    <
tr>
      <
td><table width="100%" border="0" cellspacing="5" cellpadding="5">
        <
tr>
          <
td><img src="imgs3/invoicelogo.gif" width="268" height="70" /></td>
        </
tr>
        <
tr>
          <
td><span class="style1">141 Shore Crescent, Belfast, BT15 4JR, United Kingdom </span></td>
        </
tr>
      </
table></td>
    </
tr>
    <
tr>
      <
td>&nbsp;</td>
    </
tr>
    <
tr>
      <
td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <
tr>
          <
td width="60%"><span class="style5"><?= $client->name; ?><br />
            
<? if($client->company){ ?><?= $client->company; ?><br /><? } ?>
            <?
= nl2br($client->name); ?><br />
            
<?= $client->city; ?>, <?= $client->postcode; ?></span></td>
          <
td width="40%"><div align="right">
        <
table width="100%" border="0" cellspacing="0" cellpadding="0">
              <
tr>
                <
td width="30%"><span class="style4">Account Number </span></td>
                <
td width="70%"><?= $client->accno; ?></td>
              </
tr>
              <
tr>
                <
td><span class="style4">Invoice Number </span></td>
                <
td><?= $invoice->invoiceno; ?></td>
              </
tr>
              <
tr>
                <
td><span class="style4">Date</span></td>
                <
td><?=
                 
$format = 'DATE_RFC822';
                
standard_date($format, $invoice->date); ?></td>
              </
tr>
            </
table>
          </
div></td>
        </
tr>
        <
tr>
          <
td colspan="2">&nbsp;</td>
        </
tr>
        <
tr>
          <
td colspan="2" valign="top"><table width="100%" border="0" cellspacing="4" cellpadding="2">
            <
tr>
              <
td bgcolor="#272627"><span class="style12">Description</span></td>
              <
td bgcolor="#272627"><span class="style12">Quantity</span></td>
              <
td bgcolor="#272627"><span class="style12">Cost</span></td>
            </
tr><?
                $query
= $this->db->query("SELECT * FROM invoiceitems WHERE invoiceno = ".$id."");
                
$messagecount = $query->num_rows();
                if (
$messagecount > 0){
                $totalcost
= '0';
                foreach (
$query->result() as $row)
                
{
                $totalcost
= $totalcost + $row->price;
                
?>
            
<tr>
              <
td width="63%"><span class="style20">
                
<?= $row->description; ?>
              
</span></td>
              <
td width="17%"><span class="style20">
                
<? if($row->quantity > 1){ ?> <?= $row->quantity; ?> @ <?= $client->currency.$row->quantity; ?> each<? } else {  ?> <?= $row->quantity; ?> <? } ?>
              
</span></td>
              <
td width="20%"><span class="style20"><?= $invoice->currency; ?>
                <?
= $row->price; ?>
              
</span></td>
            </
tr>
            <
tr>
              <
td>&nbsp;</td>
              <
td><div align="right" class="style20"><strong>Total Cost: </strong></div></td>
              <
td><span class="style20"><?= $invoice->currency; ?>
                <?
= $totalcost; ?>
              
</span></td>
            </
tr>
            
<? } ?>
          
</table></td>
        </
tr>
        <
tr>
          <
td colspan="2">&nbsp;</td>
        </
tr>
        <
tr>
          <
td colspan="2"><table width="100%" border="0" cellspacing="3" cellpadding="3">
            <
tr>
              <
td><span class="style22">Invoice Status: </span></td>
              <
td><span class="style20"><?= $invoice->status; ?></span></td>
            </
tr>
            <
tr>
              <
td width="18%"><span class="style22">Payment Information: </span></td>
              <
td width="82%"><span class="style20"><?= $client->terms; ?></span></td>
            </
tr>
          </
table></td>
        </
tr>
        <
tr>
          <
td colspan="2">&nbsp;</td>
        </
tr>
      </
table></td>
    </
tr>
    <
tr>
      <
td height="60" bgcolor="#0054A6"><div align="center" class="style2">www.3beem.com</div></td>
    </
tr>
  </
table>
</
div>
</
body>
</
html>

Profile
 
 
Posted: 03 February 2007 02:38 AM   [ Ignore ]   [ # 33 ]  
Summer Student
Avatar
Total Posts:  15
Joined  09-27-2006

*nested tables* are not supported by dompdf

Profile
 
 
Posted: 03 February 2007 05:59 AM   [ Ignore ]   [ # 34 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

ahhh… and theres me trying to stay away from css cause it cant support it fully…. Ill rebuild those pages… thanks xeron

Profile
 
 
Posted: 03 February 2007 08:22 AM   [ Ignore ]   [ # 35 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
3Beem.com - 02 February 2007 02:42 PM
Anyone here got a viewfile that pulls information from the database and outputs the HTML to PDF??

Nobody should have a view that pulls information from a database. It’s Bad Design. The MVC Police will torture you.

Profile
 
 
Posted: 03 February 2007 08:55 AM   [ Ignore ]   [ # 36 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

Im not sure of any other way around it, I have a table invoices and a table invoiceitems so the invoice will call the items for the related invoice before displaying it…

Is there any other easier way of doing it?

Profile
 
 
Posted: 03 February 2007 09:31 AM   [ Ignore ]   [ # 37 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

I’m not quite as sold on MVC as some of the folks around here. I know it works great most of the time, but I can’t think of any way MVC would make your task easier.

I’ll be watching to see if someone can give an example of something that would be “easier” or at least not a lot more difficult.

What I would probably do is have a model do a join on the invoice and invoice items table, producing the data, then have the controller call another model (or helper) that converts the data into an HTML string, complete with headings and table tags with css, then sends that as a lump to the view.

But separating those two models is probably not “easier” at all. Safer, perhaps. More likely to produce reusable code, perhaps.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 17 February 2007 12:34 PM   [ Ignore ]   [ # 38 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

Can anyone provide me with an example view for the pdf output?? Im having problems getting mine to work.

Thanks

Profile
 
 
Posted: 17 February 2007 12:55 PM   [ Ignore ]   [ # 39 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6593
Joined  03-23-2006

Here’s what BambooInvoice uses

function pdf()
    
{
        $this
->load->plugin('to_pdf');
        
$this->load->model('settings_model', '', TRUE);
        
$this->load->helper('typography');
        
        
$data['pageTitle'] = 'invoice';
        
        
$id = (int) $this->uri->segment(3);
        
$invoiceInfo = $this->invoices_model->getSingleInvoice($id);
        if (
$invoiceInfo->num_rows() == 0) {redirect('invoices/', 'location');}
        $data[
'row'] = $this->invoices_model->getSingleInvoice($id)->row();
        
        
$data['companyInfo'] = $this->settings_model->getCompanyInfo()->row();

        
$html = $this->load->view('invoices/pdf', $data, true);
        
pdf_create($html, 'invoice_'.$data['row']->invoiceNumber);
    
}


and the view

<?php
/*
*
*  This file is essentially a stripped down version of /views/invoices/view.php
* Any changes you make to that formatting, you may consider adding to this.
*
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>BambooInvoice:
<?= $pageTitle;?>
</title>
<
style type="text/css">
/*
* Invoice view styles notes
*
* This file NEEDS a locally located stylesheet to generate the appropriate formatting for
* transformation into a PDF.  If you alter this file (and you are encouraged to do so) just
* keep in mind that all of your formatting must be located here.  You might also find that
* there is limited or no support for a specific CSS style you want (ie: floating) and you'll
* need to work around with old-school tables.  Sorry for that... ;)  
*
*/

body {
    margin
: 0.5in;
}
h1
, h2, h3, h4, h5, h6, li, blockquote, p, th, td {
    font
-family: Helvetica, Arial, Verdana, sans-serif; /*Trebuchet MS,*/
}
h1
, h2, h3, h4 {
    color
: #5E88B6;
    
font-weight: normal;
}
h4
, h5, h6 {
    color
: #5E88B6;
}
h2 {
    margin
: 0 auto auto auto;
    
font-size: x-large;
}
h3 {
    border
-top: 1px solid #CCC;
}
li
, blockquote, p, th, td {
    font
-size: 80%;
}
ul {
    
list-style: url(/img/bullet.gif) none;
}
#footer {
    
border-top: 1px solid #CCC;
    
text-align: right;
}
</style>
</
head>
<
body>
<
table width="100%">
  <
tr>
    <
td width="60%"><p><strong>Invoice <?= $row->invoiceNumber;?></strong><br />
        <
strong><?= date('F d, Y', mysql_to_unix($row->dateIssued));?></strong></p>
    </
td>
    <
td><h2><img src="<?=base_url();?>img/logo/logo.jpg" alt="<?= $companyInfo->companyName;?>" /></h2>
      <
p>
        
<?= $companyInfo->address1;?>
        <?php
if ($companyInfo->address2 != '') {echo ', ' . $companyInfo->address2;}?><br />
        
<?= $companyInfo->city;?>,
        
<?= $companyInfo->province;?>,
        
<?= $companyInfo->country;?>
        <?
= $companyInfo->postalCode;?><br />
        
<?= auto_link(prep_url($companyInfo->website));?>
      
</p>
    </
td>
  </
tr>
</
table>
<
h3>Bill to
  <?
= $row->name;?>
</h3>
<
p>
  
<?= $row->address1;?>
  <?php
if ($row->address2 != '') {echo ', ' . $row->address2;}?>
  
<br />
  
<?= $row->city;?>,
  
<?= $row->province;?>,
  
<?= $row->country;?>
  <?
= $row->postalCode;?><br />
  
<?= auto_link(prep_url($row->website));?>
</p>
<
h4>Description of Work</h4>
<?= auto_typography($row->workDescription);?>
<p>Amount: $
  
<?= $row->amount;?>
  <?php
if ($row->tax1_rate != 0) {
    
echo '<br />' . $row->tax1_desc." (" . $row->tax1_rate . "%): $". number_format(($row->amount*$row->tax1_rate)/100, 2, '.', '');
}
if ($row->tax2_rate != 0) {
    
echo '<br />' . $row->tax2_desc." (" . $row->tax2_rate . "%): $". number_format(($row->amount*$row->tax2_rate)/100, 2, '.', '') ;
}
if ($row->tax1_rate == 0 && $row->tax2_rate == 0) {
    
echo '<br /><em>(' . $row->name . " is tax exempt.)</em>";
}
?>
  
<br />
  
Total: $
  
<?= number_format((($row->amount*$row->tax1_rate)/100)+(($row->amount*$row->tax2_rate)/100)+$row->amount, 2, '.', '') ;?>
</p>
<
p><strong>Payment Terms:
  
<?= $row->paymentTerm;?>
  
</strong></p>
<
p>
  
<?= $row->invoiceNote;?>
</p>
<
div id="footer">
  <
p>Generated by
    <?php $this
->load->view('includes/logo.inc.php');?>
    
- <a href="http://www.bambooinvoice.org/">http://www.bambooinvoice.org</a></p>
</div>
</
body>
</
html>

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 17 February 2007 01:23 PM   [ Ignore ]   [ # 40 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  01-21-2007

Thanks Derek, Ill try this and hope for the best :D

Profile
 
 
Posted: 13 March 2007 04:27 PM   [ Ignore ]   [ # 41 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Great stuff here Derek, you saved me a few hours of work. The only thing I find disappointing is that DOMPDF handles UTF-8 characters so badly. I think this is a shortcoming of PHP, not DOMPDF, though.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 15 March 2007 09:03 AM   [ Ignore ]   [ # 42 ]  
Grad Student
Rank
Total Posts:  49
Joined  02-12-2007

function pdf()
{
     $this
->load->plugin('to_pdf');
     
// page info here, db calls, etc.     
     
$html = $this->load->view('controller/viewfile', $data, true);
     
pdf_create($html, 'filename');
}

How and where do I call this function?
What’s the 3rd argument in the load->view for?
I assume that ‘controller’, ‘viewfile’ and ‘filename’ can be anything, yes?
$data, is required? According to the errors I had, I think so.

Does anybody have a simple, working, example of a controller and a view for this?

I tried a simple test:

class Dompdftest extends Controller {

    
function Dompdftest(){
        parent
::Controller();
    
}
    
function pdf(){
        $this
->load->plugin('to_pdf');
        
$data['title'] = 'The title of the page';
        
$data['content'] = 'Some content...';
        
$html = $this->load->view('dompdftest_view', $data, true);
        
pdf_create($html, 'my_filename',true);
    
}
   
}

and the view :

<body>
    <
h3><?=$title;?></h3>
    <
p><?=$content;?></p>
</
body>
</
html>

This is the error I get:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 1572864 bytes)

Profile
 
 
Posted: 15 March 2007 12:18 PM   [ Ignore ]   [