Coder Social home page Coder Social logo

utils's Introduction

Dompdf

Build Status Latest Release Total Downloads License

Dompdf is an HTML to PDF converter

At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

This document applies to the latest stable code which may not reflect the current release. For released code please navigate to the appropriate tag.


Check out the demo and ask any question on StackOverflow or in Discussions.

Follow us on Twitter.


Features

  • Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
  • Supports most presentational HTML 4.0 attributes
  • Supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
  • Supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
  • Image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)
  • No dependencies on external PDF libraries, thanks to the R&OS PDF class
  • Inline PHP support
  • Basic SVG support (see "Limitations" below)

Requirements

  • PHP version 7.1 or higher
  • DOM extension
  • MBString extension
  • php-font-lib
  • php-svg-lib

Note that some required dependencies may have further dependencies (notably php-svg-lib requires sabberworm/php-css-parser).

Recommendations

  • GD (for image processing)
    • Additionally, the IMagick or GMagick extension improves image processing performance for certain image types
  • OPcache (OPcache, XCache, APC, etc.): improves performance

Visit the wiki for more information: https://github.com/dompdf/dompdf/wiki/Requirements

About Fonts & Character Encoding

PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. These fonts only support Windows ANSI encoding. In order for a PDF to display characters that are not available in Windows ANSI, you must supply an external font. Dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and reference in CSS @font-face rules. See the font overview for more information on how to use fonts.

The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans). The following DejaVu 2.34 fonts are available: DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono.

Easy Installation

Install with composer

To install with Composer, simply require the latest version of this package.

composer require dompdf/dompdf

Make sure that the autoload file from Composer is loaded.

// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
require 'vendor/autoload.php';

Download and install

Download a packaged archive of dompdf and extract it into the directory where dompdf will reside

Use the packaged release autoloader to load dompdf, libraries, and helper functions in your PHP:

// include autoloader
require_once 'dompdf/autoload.inc.php';

Note: packaged releases are named according using semantic versioning (dompdf_MAJOR-MINOR-PATCH.zip). So the 1.0.0 release would be dompdf_1-0-0.zip. This is the only download that includes the autoloader for Dompdf and all its dependencies.

Install with git

From the command line, switch to the directory where dompdf will reside and run the following commands:

git clone https://github.com/dompdf/dompdf.git
cd dompdf/lib

git clone https://github.com/PhenX/php-font-lib.git php-font-lib
cd php-font-lib
git checkout 0.5.1
cd ..

git clone https://github.com/PhenX/php-svg-lib.git php-svg-lib
cd php-svg-lib
git checkout v0.3.2
cd ..

git clone https://github.com/sabberworm/PHP-CSS-Parser.git php-css-parser
cd php-css-parser
git checkout 8.1.0

Require dompdf and it's dependencies in your PHP. For details see the autoloader in the utils project.

Framework Integration

Quick Start

Just pass your HTML in to dompdf and stream the output:

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

Setting Options

Set options during dompdf instantiation:

use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('defaultFont', 'Courier');
$dompdf = new Dompdf($options);

or at run time

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->setDefaultFont('Courier');
$dompdf->setOptions($options);

See Dompdf\Options for a list of available options.

Resource Reference Requirements

In order to protect potentially sensitive information Dompdf imposes restrictions on files referenced from the local file system or the web.

Files accessed through web-based protocols have the following requirements:

  • The Dompdf option "isRemoteEnabled" must be set to "true"
  • PHP must either have the curl extension enabled or the allow_url_fopen setting set to true

Files accessed through the local file system have the following requirement:

  • The file must fall within the path(s) specified for the Dompdf "chroot" option

Limitations (Known Issues)

  • Table cells are not pageable, meaning a table row must fit on a single page.
  • Elements are rendered on the active page when they are parsed.
  • Embedding "raw" SVG's (<svg><path...></svg>) isn't working yet, you need to either link to an external SVG file, or use a DataURI like this:
    $html = '<img src="data:image/svg+xml;base64,' . base64_encode($svg) . '" ...>';
    Watch #320 for progress
  • Does not support CSS flexbox.
  • Does not support CSS Grid.
  • A single Dompdf instance should not be used to render more than one HTML document because persisted parsing and rendering artifacts can impact future renders.

Donate button

If you find this project useful, please consider making a donation. Any funds donated will be used to help further development on this project.)

utils's People

Contributors

bsweeney avatar fdjkgh580-2 avatar jeremy-seipelt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

utils's Issues

git issue tracking custom fonts

I'm wondering how would I track custom fonts in git. The best practice is to gitignore the whole ./vendor directory, but all the custom fonts are saved under ./vendor/dompdf/dompdf/lib/fonts, which creates an issue for me. Is there a way to move the fonts directory out of ./vendor/dompdf?

errors in load_font.php

I try to add cyrillic font to DOMPDF 0.7 but always errors.

First of all I had this when run load_font.php in command line

Fatal error: Uncaught exception 'Dompdf\Exception' with message 'Unable to read '/vendor/dompdf/dompdf/arialuni.ttf'.' in D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\load_font.php:85
Stack trace:
#0 [internal function]: install_font_family(Object(Dompdf\Dompdf), 'Arialuni', '/vendor/dompdf/...')
#1 D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\load_font.php(200): call_user_func_array('install_font_fa...', Array)
#2 {main}
  thrown in D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\load_font.php on line 85

Then I go to autoload.inc.php and found that there is

require_once __DIR__ . '/lib/php-font-lib/src/FontLib/Autoloader.php';
require_once __DIR__ . '/lib/php-svg-lib/src/autoload.php';

But checking manually I didn't find these folders under lib

ั‘@OFFICE d:\OpenServer\domains\site.new\vendor\dompdf\dompdf\lib
> ls -a
.  ..  Cpdf.php  fonts  html5lib  res

Then I commented these lines, but one more error

> php load_font.php Arialuni arialuni.ttf
Unable to find bold face file.
Unable to find italic face file.
Unable to find bold_italic face file.
Copying arialuni.ttf to D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\lib\fonts/arialuni.ttf...
Generating Adobe Font Metrics for D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\lib\fonts/arialuni...

Fatal error: Class 'FontLib\Font' not found in D:\OpenServer\domains\site.new\vendor\dompdf\dompdf\load_font.php on line 153

load_font lost previous fonts

Hi,
While trying to add several fonts to my projet I discover that load_font.php create the file dompdf_font_family_cache.php. This file is then used while rendering.

So when I added a second font with load_font.php then the file dompdf_font_family_cache.php missing the first added font, and render missing this first font.

How do you manage this case ?

The only solution I found is, before using load_font, copying dompdf_font_family_cache.php, then extract previous font definition, then copy it in the newly generated dompdf_font_family_cache.php.

Cheers.

PHP Fatal error

php load_font.php system_fonts
PHP Fatal error: Using $this when not in object context in E:\scsaas\vendor\dompdf\dompdf\load_font.php on line 180

Font manager assumes font cache is same as font directory

The code in /www/fonts.php assumes the font cache file is located in the same folder as the fonts (basically the Installed Fonts list is a list of all files within the /lib/fonts/ folder). It doesn't account for the cache folder being elsewhere (as set in the custom config.

Sabberworm namespace issue in packaged releases (autoload.inc.php)

I'm creating a stand alone server project that may or may-not allow multiple, selectable versions of dompdf. I'm using the pre-packaged releases, but the autoload.inc.php doesn't seem to handle Sabberworm namespaces properly

Two simple fixes:

  • The filenames seem to no longer have Sabberworm/CSS/ in the paths.
  • The php-css-parser/lib/ has changed to src/
  // Sabberworm
  spl_autoload_register(function($class)
  {
      if (strpos($class, 'Sabberworm') !== false) { 
          $file = str_replace('\\', DIRECTORY_SEPARATOR, $class);
          $file = str_replace('Sabberworm/CSS/', '', $file);                                                                    // added
          $file = realpath(__DIR__ . '/lib/php-css-parser/src/' . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');  // changed
          if (file_exists($file)) { 
              require_once $file;
              return true;
          } 
      } 
      return false;
  });

I would create a PR, but I cannot find this file or one that generates it.

This is low priority because I can simply include a patched copy of this autoload.inc.php. I've found that this seems to affect 1.1.1 and 1.2.0

P.S., the second str_replace should probably use DS constant or happen before the first str_replace.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.