 |
| Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 28th, 2009, 10:33 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
|
|
FPDF Multicell layout
Chaps,
After spending some time investigating component less PDF creation, i have been developing with FPDF which i am sure some of you may have used or had dealings with.
What i am trying to achieve is a results table which allows me to text wrap and insert line breaks.
With the Use of the pdf.Cell function it is fairly simple to create a drop down list
Code:
<%
'### Include Pages ###
%>
<!--#include file="fpdf.asp"-->
<%
Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF()
pdf.SetPath("fpdf/")
pdf.SetSubject ("Results")
pdf.SetTitle("Results")
pdf.SetLeftMargin(40)
pdf.Open()
pdf.AddPage()
%>
<%
'Set table header font
pdf.SetFont "Arial","B",8
'Create Header Cells
pdf.Cell 20,5,"Outcome",1,0,"C"
pdf.Cell 47,5,"Question Asked",1,0,"C"
pdf.Cell 47,5,"Answer Given",1,0,"C"
pdf.Cell 20,5,"Status",1,0,"C"
'Add Line Break
pdf.Ln()
'Set font for results
pdf.SetFont "Arial","",8
Dim a
'Create results loop
For a = 0 to 10
'Example Data
oc="1A"
qa="Does This Work"
ag="Yes"
st="Correct"
'Result cells
pdf.Cell 20,5,""&oc&"",1,0,"C"
pdf.Cell 47,5,""&qa&"",1,0,"C"
pdf.Cell 47,5,""&ag&"",1,0,"C"
pdf.Cell 20,5,""&st&"",1,0,"C"
'Add Line Break
pdf.Ln()
Next
'Write Output
pdf.Output()
%>
The issue is that pdf.Cell does not allow text wrap as the text just continues into the next cell or both sides if centred and as far as i can determine there is no way to add a line break.
Both text wrap and addition of line breaks is possible using the pdf.MultiCell layout but this come with it's own challenges and where my shove stick in hole approach is sadly failing me.
Essentially each MultiCell needs to be plotted on the page using pdf.SetXY - Bad example provided below. It works but suspect i have the pdf.MultiCell co-ordinates wrong.
It is possible to retrive the current x and y co-ordinates with pdf.GetX() and pdf.Gety().
Code:
<!--#include file="fpdf.asp"-->
<%
Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF()
pdf.SetPath("fpdf/")
pdf.SetSubject ("Results")
pdf.SetTitle("Results")
pdf.SetLeftMargin(35)
pdf.SetAutoPageBreak(0)
pdf.Open()
pdf.AddPage()
%>
<%
'List Header Font
pdf.SetFont "Arial","B",10
'Header Cells
pdf.SetXY 24,24
pdf.MultiCell 24,5,"Data1",1, "C",0
pdf.SetXY 48,24
pdf.MultiCell 28,5,"Data2",1, "C",0
pdf.SetXY 76,24
pdf.MultiCell 28,5,"Data3",1, "C",0
'Results font
pdf.SetFont "Arial","",10
'Result Cells
pdf.SetXY 24,29
pdf.MultiCell 24,5,"Res1",1, "C",0
pdf.SetXY 48,29
pdf.MultiCell 28,5,"Res2",1, "C",0
pdf.SetXY 76,29
pdf.MultiCell 28,5,"Res3",1, "C",0
pdf.Output()
%>
The issue i am having is the calculation for the looped results cell co-ordinates and also where a results cell has multiple lines of content, only that cell expands to the new height and not the rest of the row.
There are several php examples but far beyond my limited light bulb.
Below is an example invoice process in php which has resolved the issue.
index.php
Code:
<?
define("FPDF_FONTPATH", "{$_SERVER['DOCUMENT_ROOT']}repository/demos/fpdf/fpdf/font/");
require("{$_SERVER['DOCUMENT_ROOT']}repository/demos/fpdf/fpdf/fpdf.php");
require ("InvoicePDF.class.php");
$pdf = new InvoicePDF();
$pdf->Output();
?>
InvoicePDF.class.php
Code:
<?
class InvoicePDF extends FPDF {
private $PG_W = 190;
public function __construct($passInYourDataHere = NULL) {
parent::__construct();
$this->LineItems();
}
public function Header() {
$this->SetFont('Arial', 'B', 16);
$this->Cell($this->PG_W, 8, "Roxxor Ltd.", 0, 0, 'C');
$this->Ln();
$this->Cell($this->PG_W, 5, "INVOICE", 0, 0, 'L');
$this->Ln(10);
$this->SetFont('Arial', 'B', 12);
$this->Cell($this->PG_W / 4, 5, "Invoice Number:", 0, 0, 'L');
$this->Cell($this->PG_W / 4, 5, "ROXXOR_001", 0, 0, 'L');
$this->Cell($this->PG_W / 4, 5, "To:", 0, 0, 'L');
$this->Cell($this->PG_W / 4, 5, "The Client", 0, 0, 'L');
$this->Ln();
$this->Cell($this->PG_W / 4, 5, "Invoice Date:", 0, 0, 'L');
$this->Cell($this->PG_W / 4, 5, date("d/m/Y", time()), 0, 0, 'L');
$this->Cell($this->PG_W / 4, 5, "Address:", 0, 0, 'L');
$this->MultiCell($this->PG_W / 4, 5, "1 Some Road\nSome Town\nPost Code", 0, 'L');
$this->Ln(10);
}
public function LineItems() {
$header = array("Description", "Qty.", "Rate", "Sub.", "VAT", "Total");
// Data
$textWrap = str_repeat("this is a word wrap test ", 3);
$textNoWrap = "there will be no wrapping here thank you";
$data = array();
$data[] = array($textWrap, 1, 50, 50, 0, 50);
$data[] = array($textNoWrap, 1, 10500, 10500, 0, 10500);
/* Layout */
$this->SetDataFont();
$this->AddPage();
// Headers and widths
$w = array(85, 25, 20, 20, 20, 20);
for($i = 0; $i < count($header); $i++) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C');
}
$this->Ln();
// Mark start coords
$x = $this->GetX();
$y = $this->GetY();
$i = 0;
foreach($data as $row)
{
$y1 = $this->GetY();
$this->MultiCell($w[0], 6, $row[0], 'LRB');
$y2 = $this->GetY();
$yH = $y2 - $y1;
$this->SetXY($x + $w[0], $this->GetY() - $yH);
$this->Cell($w[1], $yH, $row[1], 'LRB');
$this->Cell($w[2], $yH, number_format($row[2], 2), 'LRB', 0, 'R');
$this->Cell($w[3], $yH, number_format($row[3], 2), 'LRB', 0, 'R');
$this->Cell($w[4], $yH, number_format($row[4], 2), 'LRB', 0, 'R');
$this->Cell($w[5], $yH, number_format($row[5], 2), 'LRB', 0, 'R');
$this->Ln();
$i++;
}
$this->Ln(10);
$this->setTextFont();
$this->Cell($w[0] + $w[1] + $w[3], 6, 'Total', 'TB', 0, 'L');
$this->setDataFont(true);
$this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R');
$this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R');
$this->Cell($w[2], 6, number_format(2, 2), 'TB', 0, 'R');
$this->Ln();
$this->setTextFont();
$this->Write(10, "Notes: " . "Thank you for your business.");
$this->Ln(10);
}
public function Footer() {
$this->Ln();
$this->Cell($this->PG_W, 5, "Payment Terms: " . "On Receipt", 0, 0, 'L');
$this->Ln(10);
$this->Cell($this->PG_W, 5, "Please make cheques payable to Roxxor Ltd.", 0, 0, 'L');
$this->Ln(10);
$this->setTextFont(true);
$this->Cell($this->PG_W, 5, "Payment Details:", 0, 0, 'L');
$this->setTextFont(false);
$this->Ln();
$this->Cell($this->PG_W, 5, "Bank A/C No: 000000000", 0, 0, 'L');
$this->Ln();
// Footer address
$address = "Roxxor Ltd.\nSomewhere in London\nUK";
$this->SetY(-(($this->getAddressLength($address) * 5) + 20));
$this->SetFont('Arial', '', 7);
$this->Ln();
$this->writeAddress($address);
}
private function setTextFont($isBold = false) {
$this->SetFont('Arial', $isBold ? 'B' : '', 9);
}
private function setDataFont($isBold = false) {
$this->SetFont('Courier', $isBold ? 'B' : '', 8);
}
private function getAddressLength($address) {
return count(explode("\n", $address));
}
private function writeAddress($address) {
$lines = explode("\n", $address);
foreach ($lines as $line) {
$this->Cell($this->PG_W, 5, $line, 0, 0, 'C');
$this->Ln(4);
}
}
}
?>
Where as this example uses a class page, i am happier to include all of the process in one page.
Any suggestions or ideas would be greatly appreciated
|
|

August 4th, 2009, 01:47 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
|
|
My humorous thought on trying to find a solution to the classic asp only examples for FPDF is that only non-English speaking forums seem interested in having a go..
I am guessing they like myself still use ASP and not the world renown PHP .. which apparently fixes everything .. at least from re-writing the FPDF library point of view ..
No swipes from cleaver people who understand PHP please .. Just joking!
So for anyone who still uses asp, the link to the FDPF files is shown below.
http://www.aspxnet.it/public/Files/Download/178/fpdf1.01.zip
So from my last clutching at straws post i have trawled the IT University of website posts and have put together in my classic aspless approach, my attempt at a FPDF multi Cell table..
So what works and what does not work?
Well as is, the page works but with a couple of annoying additions .. Firstly the Pdf.Nblines function found in the Tablemulticell.ext seems to add one to many lines in it's calculations for the first row only.
Secondly is the page Break .. If left out the first page correctly closes the table and then adds one cell to the next page, the second cell on the next page, third on next and from the fourth page on-wards re-creates the table until it reaches the end of the page and repeats... So just need to write a better page function.
Below is my example MultiCell table -
Code:
<!--#include file="fpdf.asp"-->
<%
'Standard FPDF elements
Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF()
pdf.SetPath("fpdf/")
'Load the Tablemulticell.ext located \fpdf\extends
pdf.LoadExtension "Tablemulticell"
pdf.SetLeftMargin(35)
pdf.Open()
pdf.AddPage()
%>
<%
'### Header Cell layout ###
'Set Header Font
pdf.SetFont "Arial","B",10
Dim x(3) 'Simple array to store the x Co-Ordinates
'### Header Layout Cells ###
'So for each MultiCell we set the x Co-ordinate (14) from left and the y Co-ordinate (24) from top
pdf.SetXY 14,24
'Next we take a reference of the first cell x Co-ordinate to use in the results MultiCell location
x(0)=pdf.GetX()
'Next we add the MultiCell which includes the width (30), height (7), Input txt, Border identifier and Centered
pdf.MultiCell 30,7,"Outcome",1, "C",0
pdf.SetXY 44,24
x(1)=pdf.GetX()
pdf.MultiCell 57,7,"Question Asked",1, "C",0
pdf.SetXY 101,24
x(2)=pdf.GetX()
pdf.MultiCell 57,7,"Answer Given",1, "C",0
pdf.SetXY 158,24
x(3)=pdf.GetX()
pdf.MultiCell 34,7,"Status",1, "C",0
%>
<%
'Create Demo Loop - Change to SQL as required
Dim z
For z = 0 to 40
'### Test Table values ###
'Outcome Ref:
Outcome="1.a"
'Question Asked Ref:
QuestAsked="This is a very ling text line that should go over at least one if not two lines or maybe three or four lines"
'Answer Given Ref:
AnsGiven="This May Not Go as far but two line at least go over at least one if not"
'Status
QuestStat="Correct"
'Calculate Number of lines using Pdf.Nblines from Tablemulticell Extension
Lg1=Pdf.Nblines(30,Outcome)
Lg2=Pdf.Nblines(57,QuestAsked)
Lg3=Pdf.Nblines(57,AnsGiven)
Lg4=Pdf.Nblines(34,QuestStat)
'Calculate Heighest Value
Nl=Lg1
If Nl<Lg2 then Nl=Lg2
If Nl<Lg3 then Nl=Lg3
If Nl<Lg4 then Nl=Lg4
'Now for the odd bit, for the first row only, calculation of Pdf.Nblines adds one too many lines so remove one!
If OccurnaceCount = "" then
'Remove the extra line
Nl = Nl - 1
'Check Counts for all Lg values and remove one line if multiple lines
If Lg1 > 1 then Lg1 = Lg1 - 1 End If
If Lg2 > 1 then Lg2 = Lg2 - 1 End If
If Lg3 > 1 then Lg3 = Lg3 - 1 End If
If Lg4 > 1 then Lg4 = Lg4 - 1 End If
End If
%>
<%
'This is a rudimentory page break - If not included the next page of cells only includes the first cell then next ect
If pdf.CheckPageBreak(y) = True then
'Find New y Co-ordinates
y=14
else
'Get Current y Co-ordinate from the bottom of the last row
y=pdf.GetY()
End If
%>
<%
'Or Just
'y=pdf.GetY()
%>
<%
'### Header Layout Cells ###
'Set Results Font
pdf.SetFont "Arial","",8
'Results Layout Cells
pdf.SetXY x(0),y
pdf.MultiCell 30,(Nl/Lg1)*5,""&Outcome&"",1, "C",0
pdf.SetXY x(1),y
pdf.MultiCell 57,(Nl/Lg2)*5,""&QuestAsked&"",1, "L",0
pdf.SetXY x(2),y
pdf.MultiCell 57,(Nl/Lg3)*5,""&AnsGiven&"",1, "L",0
pdf.SetXY x(3),y
pdf.MultiCell 34,(Nl/Lg4)*5,""&QuestStat&"",1, "C",0
'Simple row count for page break
If OccurnaceCount = "" then
OccurnaceCount = 1
else
OccurnaceCount = OccurnaceCount + 1
End If
%>
<%
Next
%>
<%
pdf.Output()
%>
If anyone has created a better ASP version of the multicell page or has worked out how to use the ROW function from Tablemulticell.ext or is willing to look at the why the above page is flawed i would greatly appreciate it.
Thanks Aspless
|
|

August 4th, 2009, 02:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
I think it's more likely that there just aren't that many people interested in producing PDF in this manner.
I know that I much prefer to simply create HTML and then "print" the result to PDF using a PDF-printer. Not that I've even had that much occasion to do that. Generally, if I need a document, I'd create a WORD document, not PDF, as 99% of the world has no way to treat PDF as anything except read-only.
|
|

August 4th, 2009, 04:50 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
|
|
I suspect you are correct Old Pedant.
In regards to the purpose, you have hit on the very reason i am taking the time to produce the PDF in this manner in the first place.
For exporting of amendable / re-sortable data i use xls mainly as this is a good interface with the end user.
How ever for non amendable , read only content such as invoices / quotes / static results / Contracts, having an automated export function for producing this data in PDF with out the need for additional client based print conversion apps (which would not be accepted by many of our customers IT policies) fits well with our customers and is regularly requested.
The other reason for automating the process is time. This function once written will quite simply reduce the amount of clicks it takes to produce the invoices / quotes that are currently created manually into PDF.
Due to the fact that we use as most an ISP, there is no option for the DLL install on the hosted IIS clusters, which would worry me any way to have a dependency of a third party component that could be removed by accident... I know :o) i've done it! lol
Lastly i suppose is i like the challenge .. If it's hard, it's worth doing!
|
|

August 5th, 2009, 01:29 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
|
|
MultiCell Table layout solved
After further investigation the basic MultiCell layout function now fully works.
One extends file that was missing from all the english forums was table.ext which does most of the complicated cell co-ordinates and text wrap / in - cell line breaks.
Below is the simple version of table.ext
Code:
var widths;
this.Table = {Border:{Width:0.1,Color:''},Fill:{Color:''},TextAlign:"J"}
this.SetColumns=function(){this.widths=arguments;}
this.HexToRGB=function(value){
var ar=new Array()
s= new String(value.toUpperCase());
ar["R"] = parseInt(s.substring(0,2),16);
ar["G"] = parseInt(s.substring(2,4),16);
ar["B"] = parseInt(s.substring(4,6),16);
return ar;
}
this.Row=function()
{
var xdata = arguments
var xi;var xh;var xnb;
var xw;
xnb=0;
for(xi=0;xi<xdata.length;xi++){xnb=Math.max(xnb,this.NbLines(this.widths[xi],xdata[xi]))};
xh=(xnb)*5;
this.CheckPageBreak(xh);
for(xi=0;xi<xdata.length;xi++)
{
xw=this.widths[xi];
xx=this.GetX();
xy=this.GetY();
if (this.Table.Border.Width>0||this.Table.Fill.Color!=''){
var xstyle='';
this.SetLineWidth(this.Table.Border.Width);
if(this.Table.Border.Color!=''){
var RGB = this.HexToRGB(this.Table.Border.Color);
this.SetDrawColor(RGB["R"],RGB["G"],RGB["B"]);
xstyle+='D';
}
if(this.Table.Fill.Color!=''){
var RGB = this.HexToRGB(this.Table.Fill.Color);
this.SetFillColor(RGB["R"],RGB["G"],RGB["B"]);
xstyle+="F"
}
this.Rect(xx,xy,xw,xh,xstyle);
}
this.MultiCell(xw,5,xdata[xi],0,this.Table.TextAlign);
this.SetXY(xx+xw,xy);
}
this.Ln(xh);
}
this.CheckPageBreak=function(xh)
{
if(this.GetY()+xh>this.PageBreakTrigger)this.AddPage(this.CurOrientation);
}
this.NbLines=function(xw , xtxt)
{
var xnb;
xcw=this.CurrentFont["cw"];
if(xw==0)xw=this.w-(this.rMargin)-this.x;
xwmax=((xw)-2*(this.cMargin))*1000/(this.FontSize);
xs=lib.str_replace("\r","",xtxt);
xnb=xs.length;
if(xnb>0 && xs.charAt(xnb-1)=="\n")xnb--;
xsep=-1;
xi=0;
xj=0;
xl=0;
xnl=1;
while(xi<xnb)
{
xc=xs.charAt(xi);
if(xc=="\n")
{
xi++;
xsep=-1;
xj=xi;
xl=0;
xnl++;
continue;
}
if(xc==" ")xsep=xi;
xl+=(xcw[xc]);
if(xl>xwmax)
{
if(xsep==-1)
{
if(xi==xj)xi++;
}
else xi=xsep+1;
xsep=-1;
xj=xi;
xl=0;
xnl++;
}
else {xi++;}
}
return xnl;
}
Lastly is the process page that creates the four collumn table
Code:
<!--#include file="fpdf.asp"-->
<%
Dim i,pdf,d
Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF()
pdf.SetPath("fpdf/")
pdf.LoadExtension("table")
pdf.Open()
pdf.AddPage()
pdf.Table.Border.Width = 0.1
pdf.Table.Border.Color="006699"
'pdf.Table.Fill.Color="C9C8C0"
pdf.Table.TextAlign = "L"
pdf.SetColumns 30,57,57,34
'pdf.SetAligns "C", "L", "L", "C"
pdf.SetFont "Arial","B",10
pdf.Row "Outcome","Question Asked","Answer Given","Status"
pdf.SetFont "Arial","",10
for i=0 to 20
a= vbcrlf & "1.a"
b="This is a very long question that will use more than one line of text"
c="A) This is answer one which should go over onle line" & vbcrlf & ""&_
"B) This is answer Two which should go over onle line" & vbcrlf & ""&_
"C) This is answer Three which should go over onle line" & vbcrlf & ""&_
"D) This is answer Four which should go over onle line" & vbcrlf & ""
d=vbcrlf & "Correct" & vbcrlf & "2 Out Of 3"
pdf.Row a,b,c,d
next
pdf.Close()
pdf.Output()
%>
There is a more complicated version of table.ext that allows further control of text alignment within the collumns but still working on this at pres!
Hope this may be of some use.
Cheers and happy coding..
Aspless
|
|
The Following User Says Thank You to aspless For This Useful Post:
|
|
|

September 27th, 2009, 02:15 AM
|
|
Registered User
|
|
Join Date: Sep 2009
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Aspless, thanks a lot, really thanks.
|
|

December 1st, 2011, 10:50 AM
|
|
Registered User
|
|
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks
Thank you Aspless. This code will be a great help for me; I tested and it worked perfect. It's just what I need. Fernando (MedellÃn, Colombia)
|
|

May 30th, 2012, 08:35 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Aspless. But it's perfect. Just one thinks, the pdf.SetAligns doesn't work. Can you tell me about this...
Thanks
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Layout Managers |
ironchef |
Java Basics |
1 |
October 20th, 2006 05:09 AM |
| Layout problem |
roy_mm |
Reporting Services |
1 |
September 28th, 2006 03:43 PM |
| Help with form layout |
djjasonsa |
Access VBA |
1 |
February 15th, 2005 12:50 AM |
| layout |
adflynn |
Java GUI |
0 |
November 9th, 2004 06:33 AM |
| Table Layout |
benskywalker |
Dreamweaver (all versions) |
3 |
October 22nd, 2003 11:48 AM |
|
 |