CSS properties supported by TCPDF
CSS Properties Supported by TCPDF
TCPDF is a PHP library for creating PDF files. It allows you to use some CSS styles when generating PDF documents. In this article, we will explain in detail the CSS properties supported by TCPDF to help you better control the document style when generating PDF files.
CSS Property List
TCPDF supports CSS properties including but not limited to the following:
font-family
: Sets the font family, such as “Helvetica”, “Arial”, “Times”, etc.font-size
: Sets the font size, which can be in pixels (px), points (pt), or percentage (%).font-style
: Sets the font style, including “normal”, “italic”, and “oblique”.font-weight
: Sets the font weight, which can be “normal” or “bold”.text-decoration
: Sets text decoration, such as “underline”, “overline”, and “line-through”.text-align
: Sets the horizontal alignment of the text, such as “left”, “right”, and “center”.color
: Sets the text color, which can be a color name or hexadecimal value.background-color
: Sets the background color, which can be a color name or hexadecimal value.line-height
: Sets the line height, which can be pixels (px), points (pt), or percentages (%).margin
: Sets the element’s margins, which can be values in the top, right, bottom, or left directions.padding
: Sets the element’s padding, which can be values in the top, right, bottom, or left directions.
These are some common CSS properties supported by TCPDF. You can also try other style properties when generating PDF files to achieve more customized results.
Sample Code
The following is a sample code for generating a PDF file using TCPDF, which includes the application of some common CSS properties:
<?php
require_once('tcpdf/tcpdf.php Tutorial">php');
<span class="katex math inline">pdf = new TCPDF();</span>pdf->AddPage();
// Set title style
<span class="katex math inline">pdf->SetFont('helvetica', 'B', 16);</span>pdf->Cell(0, 10, 'Hello, TCPDF!', 0, 1, 'C');
// Set paragraph style
<span class="katex math inline">pdf->SetFont('helvetica', '', 12);</span>pdf->SetTextColor(255, 0, 0);
<span class="katex math inline">pdf->Cell(0, 10, 'This is a sample text with different styles.', 0, 1, 'L', false, '', 0, false, 'T', 'C');
//Set the list style</span>pdf->SetFont('helvetica', '', 10);
<span class="katex math inline">pdf->SetTextColor(0, 0, 255);</span>html = '<ul><li>List item 1</li><li>List item 2</li><li>List item 3</li></ul>';
<span class="katex math inline">pdf->writeHTMLCell(0, 0, 10, 40,</span>html, 0, 1, 0, true, 'L', false);
$pdf->Output('sample.pdf', 'I');
?>
In the sample code above, we used the SetFont
method to set different fonts, weights, and styles, the SetTextColor
method to set different colors, and the writeHTMLCell
method to insert text content containing HTML tags.
Running Results
When you run the sample code above, a PDF file named sample.pdf
will be generated, containing text and list items in different styles. The results are as follows:
- Heading Style: Hello, TCPDF!
- Paragraph Style: This is a sample text with different styles.
- List Style:
- List Item 1
- List Item 2
- List Item 3
By flexibly utilizing the CSS properties supported by TCPDF, you can achieve richer styling effects when generating PDF files, improving the readability and aesthetics of your documents.