The post Creating PDF Files in PHP by Using the FPDF Library introduces the basic steps of how to create a PDF file by using the free FPDF library. Here, we take drawing dash lines as an example to show you how to extend the FPDF library and write your own functions for generating PDF files.
The PDF file format uses various operators to describe graphic and text objects. Details of the operator definitions can be referred to the Adobe PDF Reference 1.7. The FPDF library actually help you generate the PDF file format by using the PDF content stream operators.
The following dash pattern format is used to set a dash line:
Dash_array Dash_phase d
where Dash_array
is an array that specifies the lengths of dashes and gaps,
Dash_phase
specifies the distance into the dash pattern at which to start the dash, and d
is the setdash
operator.
Referring to the Adobe PDF reference 1.7, it provides the following examples of line dash patterns.
The pattern [] 0
defines a solid line with no dash. The pattern [3] 0
defines 3 units of dash and 3 units of gap starting from 0
unit. And the pattern [2 1] 0
defines 2 units of dash and 1 unit of gap starting from 0
unit. When the Dash_phase
is bigger than 0
, for example, the pattern [3 5] 6
defines the pattern starting from the sixth unit with a gap. Therefore, the units from 3
to 5
should be a dash and the unit from 1
to 2
should be a gap.
To draw the dash line, we need to create a new class that extends the class FPDF
in a new PHP file, for example, it is called mydrawing.php.
require('.\fpdf183\fpdf.php');
class MyDrawing extends FPDF
{
function SetDash($pattern = null, $black = null, $white = null)
{
// k is the scale factor (number of points in user unit)
$s = sprintf($pattern, $black*$this->k, $white*$this->k);
$this->_out($s);
}
function SetLineWidth($width = 1)
{
$s = sprintf('%.2F w', $width);
$this->_out($s);
}
}
In the above code snippet, the new class, MyDrawing
, is created and
it extends from the FPDF
.
The function SetDash()
sets the dash pattern regarding the input dash pattern format
$pattern
, the number of the dash units $black
and
the number of the gap units $white
.
k
is the protected variable in the class FPDF
for
the scale factor. After setting the dash pattern, the protected function
_out()
is called to add a new line to the document.
$this->_out($s);
The function SetLineWidth()
is used to set the line width in the document. By default, the line width is set as 1
.
It uses the PDF's setlinewidth operator w
as defined in the code:
$s = sprintf('%.2F w', $width);
The following codes show how to draw the dash line:
require('mydrawing.php');
$pdf = new MyDrawing();
$pdf->AddPage();
$pdf->SetDash('[%.2F %.2F] 0 d', 1, 1);
$pdf->Line(20, 10, 190, 10);
$pdf->SetLineWidth(2);
$pdf->SetDash('[%.2F %.2F] 0 d', 3, 1);
$pdf->Line(20, 20, 190, 20);
$pdf->SetDash('[] 0 d');
$pdf->SetLineWidth();
$pdf->Line(20, 30, 190, 30);
$pdf->Output();
The code
$pdf->SetDash('[%.2F %.2F] 0 d', 1, 1);
is used to set the dash pattern as 1 unit of dash and 1 unit of gap. The code
$pdf->SetDash('[%.2F %.2F] 0 d', 3, 1);
sets the dash pattern as 3 units of dash and 1 unit of gap. And the code
$pdf->SetDash('[] 0 d');
is used to draw a solid line. The output of the PDF file will look like this:
References
Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com