yetiforce/yetiforcepdf

Library that generate pdf files from html.

Fund package maintenance!
Open Collective

Installs: 125 386

Dependents: 1

Suggesters: 0

Security: 0

Stars: 14

Watchers: 13

Forks: 7

0.1.42 2024-09-09 11:46 UTC

README

Latest Stable Version Build Status Scrutinizer Code Quality Maintainability

PDF generation library for PHP

The best library in the world to generate PDF from HTML

Issues & bugs

Report errors related to PDF in https://github.com/YetiForceCompany/YetiForceCRM/issues

Basic usage (for more take a look at examples folder)

<?php
$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml('<div style="text-align:center;">your html goes here</div>');
$pdfFile = $document->render();
file_put_contents('YourPDF.pdf', $pdfFile);

Supported css rules

  • font-family
  • font-size
  • font-weight
  • font-style
  • margin [px, em, cm, mm, pt, in]
  • margin-left [px, em, cm, mm, pt, in]
  • margin-top [px, em, cm, mm, pt, in]
  • margin-right [px, em, cm, mm, pt, in]
  • margin-bottom [px, em, cm, mm, pt, in]
  • padding [px, em, cm, mm, pt, in]
  • padding-left [px, em, cm, mm, pt, in]
  • padding-top [px, em, cm, mm, pt, in]
  • padding-right [px, em, cm, mm, pt, in]
  • padding-bottom [px, em, cm, mm, pt, in]
  • border [px, em, cm, mm, pt, in]
  • border-left
  • border-top
  • border-right
  • border-bottom
  • border-left-width [px, em, cm, mm, pt, in]
  • border-top-width [px, em, cm, mm, pt, in]
  • border-right-width [px, em, cm, mm, pt, in]
  • border-bottom-width [px, em, cm, mm, pt, in]
  • border-color [hex, rgb]
  • border-left-color [hex, rgb]
  • border-top-color [hex, rgb]
  • border-right-color [hex, rgb]
  • border-bottom-color [hex, rgb]
  • border-style
  • border-left-style
  • border-top-style
  • border-right-style
  • border-bottom-style
  • box-sizing
  • display [block, inline, inline-block, none]
  • width [%, px, em, cm, mm, pt, in]
  • height [%, px, em, cm, mm, pt, in]
  • overflow
  • vertical-align [baseline, top, bottom, middle]
  • line-height [px, em, cm, mm, pt, in]
  • background-color [hex, rgb]
  • color [hex, rgb, rgba, named]
  • word-wrap [normal]
  • max-width
  • min-width
  • white-space [normal]
  • word-wrap
  • float
  • clear
  • table displays
  • background-image
  • rgb / rgba color

Available fonts by default

  • DejaVu Sans

Custom fonts

Upload your ttf font files to server and add following code at the beginning of your html.

Each font weight and style could be defined if your font supports it.

<div data-font data-family="Your Font Name" data-weight="400" data-style="normal" data-file="/absolute/path/to/font/file/YourFontName-Regular.ttf"></div>
<div data-font data-family="Your Font Name" data-weight="400" data-style="italic" data-file="/absolute/path/to/font/file/YourFontName-Italic.ttf"></div>
<div data-font data-family="Your Font Name" data-weight="800" data-style="normal" data-file="/absolute/path/to/font/file/YourFontName-Bold.ttf"></div>
<div data-font data-family="Your Font Name" data-weight="800" data-style="italic" data-file="/absolute/path/to/font/file/YourFontName-BoldItalic.ttf"></div>

You can also add fonts inside php like:

\YetiForcePDF\Document::addFonts([
	[
		'family' => 'Pacifico',
		'weight' => '400',
		'style' => 'normal',
		'file' => '/absolute/path/to/Pacifico/Pacifico-Regular.ttf'
	],
	[
		'family' => 'Lobster Two',
		'weight' => '400',
		'style' => 'normal',
		'file' => '/absolute/path/to/Lobster_Two/LobsterTwo-Regular.ttf'
	],
	[
		'family' => 'Lobster Two',
		'weight' => 'bold',
		'style' => 'normal',
		'file' => '/absolute/path/to/Lobster_Two/LobsterTwo-Bold.ttf'
	],
]);

Headers, footers and watermarks

You can define header, footer and watermark to be displayed inside pdf file

<div data-header>
    <table style="border:1px solid greenyellow;font-size:12px;margin:10px;background-color:yellow;width:100%">
        <tr>
            <td style="width:50%;border:1px solid red;">This is a page header</td>
            <td style="text-align:right;border:1px solid red;">which could be table</td>
        </tr>
    </table>
</div>
<div data-footer>
    <div style="border:1px solid greenyellow;font-size:14px;margin:10px;">
        <div style="text-align:center">Footer</div>
    </div>
</div>
<div data-watermark style="text-align:center;">
    <div style="display:inline-block;font-size:20px;opacity:0.25;">
        <img src="Image.jpg">
    </div>
</div>

Page format, orientation and margins

Page format, orientation and margins could be defined from YetiForcePDF instance or inside html code.

If you have multiple page formats and orientations inside one pdf document you can insert multiple data-page-groups to your html.

<div data-page-group
     data-format="A4"
     data-orientation="P"
     data-margin-left="30"
     data-margin-right="30"
     data-margin-top="40"
     data-margin-bottom="40"
     data-header-top="10"
     data-footer-bottom="10"
></div>

Page break

When you want to add new page use data-page-group or set style on div element page-break-after:always.

<div style="page-break-after:always;"></div>

Page numbering

When you want to place page number (in header or footer for example) you can do it by placing {p} or {a} text inside your html - it will be replaced by {p} page number or {a} all pages count.

<div data-footer>
    <div style="border:1px solid greenyellow;font-size:14px;margin:10px;">
        <div style="text-align:center">Page {p} / {a}</div>
    </div>
</div>

Barcodes

<div data-barcode="EAN13" data-size="1" data-height="16" data-code="9780691147727"></div>

TODO (later but important)

  • floats
  • display: list-item
  • basic transformations (rotateXY, scaleXY, translateXY)
  • position (absolute, relative)

License

Distributed under the MIT license. See LICENSE for details.

👥 Contributors

This project exists thanks to all the people who contribute.