Problem with FPDF showing date correctly

Archived from the Xataface Users forum.

tomhousley — Tue Jul 07, 2009 8:00 am

I have succesfully created action etc to use FPDF to create pdfs from content in my database.

However, I’m not able to get it to print dates correctly.

Here is print_this_action.php

The

Code: Select all
import('actions/pdf.php');

is another php file I use for the headers & footers.

Code: Select all
`<?php
define(‘FPDF_FONTPATH’,’actions/font/’);
require(‘actions/fpdf.php’);
import(‘actions/pdf.php’);
class dataface_actions_print_this_action {
function handle(&$params){
$app =& Dataface_Application::getInstance();
$record =& $app->getRecord();

$pdf_body = $record->val(‘cor_body’);
$pdf_date = $record->val(‘cor_date’);

$pdf=new PDF();

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont(‘arial’,’‘,10);
$pdf->SetLeftMargin(20);
$pdf->SetRightMargin(20);

$pdf->Cell(190,4,’‘,0,1,’L’,0);
$pdf->Ln(20);

$pdf->Cell(10,20,$pdf_date,0,1,’L’,0);

$pdf->Ln(20);

$pdf->MultiCell(0,5,$pdf_body,0,1);
$pdf->Output();
}
}
?>`

The $pdf_body prints fine. the $pdf_date shows up in the PDF as just the word Array.

Any help would be greatly appreciated.

Many thanks, Tom


shannah — Tue Jul 07, 2009 1:05 pm

You should use the strval() method of Dataface_Record rather than val(). Some types of data are stored internally as arrays or objects. In particular date fields are stored as arrays. Using strval() ensures that you will get the string version.

-Steve


tomhousley — Tue Jul 07, 2009 2:24 pm

Thank you Steve,

One more question… how do i format the date, currently it looks like:

2009-07-16 21:13:00

… I would like something like:

16 July 2009

Can’t get my head round how the date thing in PHP works in this.

Currently have:

Code: Select all
$pdf_dmy = $record->strval('cor_date');
Code: Select all
$pdf->Cell(165,5,$pdf_dmy,0,1,'R',0);

Thank you for your help, Tom.


shannah — Tue Jul 07, 2009 3:06 pm

See http://ca.php.net/manual/en/function.date.php

for date formatting options:

You would do something like:

Code: Select all
date('Y-m-d', strtotime($record->strval('mydatefield')));

-Steve


shannah — Tue Jun 26, 2012 10:55 pm

You can’t write HTML directly into a PDF. I think that libraries like FPDF may support limited HTML styling of text in certain structures (e.g. Multicell), but certainly not full HTML.

If you want to layout your PDF using HTML and CSS, you may want to look at a library like dompdf instead.

-Steve


cookie720 — Wed Jun 27, 2012 12:08 am

tcpdf has a write html function TCPDF::writeHTML which supports li div, and even img…..but im getting an error

Code: Select all
TCPDF ERROR: [Image] Unable to get image: http://localhost/live_matters/xataface/images/document_icon.gif

shannah — Wed Jun 27, 2012 1:29 pm

Is that URL working?


cookie720 — Wed Jun 27, 2012 5:30 pm

yeah it is! but im not even sure why it would need that image, i was trying to display a BLOB field