What CSS styles does TCPDF support?

Which CSS styles does TCPDF support?

Which CSS styles does TCPPDF support?

Introduction

TCPDF is a PHP library for creating PDF files. It allows users to use some CSS styles to control the style of text and elements when generating PDF files. In this article, we will introduce in detail the CSS styles supported by TCPDF and how to use them to create PDF files with various styles.

Supported CSS styles

TCPDF supports the following CSS styles:


  1. font-family: Sets the text’s font style. You can use a web-safe font or import a custom font file.
  2. font-size: Sets the text’s font size. You can use pixels, percentages, or points.
  3. font-style: Sets the text’s font style. You can use normal, italic, or oblique.
  4. font-weight: Sets the text’s font weight. You can use normal, bold, or a number.
  5. text-align: Sets the text’s alignment. You can use left, right, center, or justify.
  6. color: Sets the text’s color. You can use an RGB value, hexadecimal value, or color name.
  7. background-color: Sets the background color of the text. You can use an RGB value, hexadecimal value, or color name.
  8. text-decoration: Sets the text decoration effect. You can use none, underline, overline, or line-through.
  9. line-height: Sets the line height of the text. You can use pixels, percentages, or numbers as units.
  10. letter-spacing: Sets the spacing between characters in the text. You can use pixels, percentages, or numbers as units.
  11. word-spacing: Sets the spacing between words in the text. You can use pixels, percentages, or numbers as units.
  12. text-transform: Sets the case transformation of the text. You can use none, uppercase, lowercase, or capitalize.

How to Use CSS Styles

To use CSS styles in TCPDF, you can follow these steps:

  1. Create a TCPDF instance:
require_once('tcpdf/tcpdf.php'); 

<span class="katex math inline">pdf = new TCPDF();</span>pdf->AddPage(); 
  1. Apply CSS styles to the text:
$html = '<p style="font-family: Arial, sans-serif; font-size: 12px; color: #333;">Hello, World!</p>'; 

$pdf->writeHTML($html, true, false, true, false, ''); 
  1. Output PDF file:
$pdf->Output('example.pdf', 'I'); 

Sample Code and Running Results

The following is a simple sample code that demonstrates how to apply CSS styles in TCPDF:

require_once('tcpdf/tcpdf.php'); 

<span class="katex math inline">pdf = new TCPDF();</span>pdf->AddPage(); 

<span class="katex math inline">html = '<p style="font-family: Arial, sans-serif; font-size: 12px; color: #333;">Hello, World!</p>';</span>pdf->writeHTML(<span class="katex math inline">html, true, false, true, false, '');</span>pdf->Output('example.pdf', 'I'); 

After running the above code, a PDF file named “example.pdf” will be generated, containing the text “Hello, World!” and with the specified CSS style applied.

Summary

In this article, we introduced the CSS styles supported by TCPDF in detail and demonstrated how to apply these styles in TCPDF to create PDF files with various styles. By using CSS styles, users can easily control the appearance of text and elements, achieving more flexible and beautiful PDF file generation.

Leave a Reply

Your email address will not be published. Required fields are marked *