Html to pdf generator in php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

OFFICIAL PROJECT | HTML to PDF converter written in PHP

License

spipu/html2pdf

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Html2Pdf is a HTML to PDF converter written in PHP, and compatible with PHP 5.6 to 8.2.

It allows the conversion of valid HTML in PDF format, to generate documents like invoices, documentation, .

You have to write a code of HTML for Html2Pdf, and not try to convert directly an already existing html page.

Specific tags have been implemented, to adapt the html standard to a PDF usage.

You must use Composer to install this library.

It uses TCPDF for the PDF part.

Html2Pdf works with PHP >5.6 and Composer.

You will also need at least the following php extensions:

You will find the install documentation here.

You will find all the documentation here.

You will find lots of examples here.

You can support this project by making a donation.

For questions and bug reports, please use the GitHub issues page.

This program is distributed under the OSL License. For more information see the ./LICENSE.md file.

Copyright 2008-2023 by Laurent Minguet

About

OFFICIAL PROJECT | HTML to PDF converter written in PHP

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Читайте также:  Pandas profiling to html

HTML to PDF converter for PHP

License

dompdf/dompdf

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

. since these should be represented in the text with the replacement character. Excludes non-printable characters.

Git stats

Files

Failed to load latest commit information.

README.md

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.

  • 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)
  • 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).

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

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.

Читайте также:  Рамка вокруг таблицы

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 a packaged archive of dompdf and extract it into the directory where dompdf will reside

  • You can download stable copies of dompdf from https://github.com/dompdf/dompdf/releases
  • Or download a nightly (the latest, unreleased code) from http://eclecticgeek.com/dompdf

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.

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.

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();

Set options during dompdf instantiation:

use Dompdf\Dompdf; use Dompdf\Options; $options = new Options(); $options->set('defaultFont', 'Courier'); $dompdf = new Dompdf($options);
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:

  • 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 ( ) isn’t working yet, you need to either link to an external SVG file, or use a DataURI like this:

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

About

HTML to PDF converter for PHP

Источник

Convert HTML to PDF in PHP the Easy Way

In this tutorial you will learn how to easily convert web pages and raw HTML documents to PDF in your PHP applications. We will use the Pdfcrowd API for PDF generation. The API offers these benefits:

  • The API is easy to use and fully supports HTML/CSS3/JavaScript.
  • The integration takes only a few minutes.
  • No third-party libraries are needed, just a single tiny PHP file.
  • It does not consume CPU/memory on your computer, PDFs are created on the Pdfcrowd servers.
Читайте также:  Проверка правильности заполнения формы html

Introduction

Let’s start with an example:

convertUrl('https://en.wikipedia.org/'); ?>

This code converts en.wikipedia.org and stores the generated PDF to a string variable. You can save the result to a file or you can stream it to the browser, which we will discuss in detail shortly. You can click the thumbnail to open the generated PDF file:

Wikipedia

Besides web pages, you can also convert a local HTML file or an HTML string:

convertFile('/path/to/your/file.html'); $pdf = $client->convertString('bold and italic'); ?>

It is also possible to save the PDF directly to a file:

convertUrlToFile('http://example.com/', 'example.pdf'); ?>

Basic Customization

Now that you know the basics, you may want to customize the generated PDF. Let’s change the page format to Letter with half-inch margins:

setPageSize("Letter"); $client->setPageMargins("0.5in", "0.5in", "0.5in", "0.5in"); ?>

You can use metric units as well:

setPageMargins("1cm", "1cm", "10mm", "10mm"); ?>

You can also specify the appearance of the PDF when it is opened in a viewer:

setInitialPdfZoomType(Pdfcrowd::FIT_PAGE); $client->setPageLayout(Pdfcrowd::CONTINUOUS); ?>

The API provides many other options including password protection and fully customizable page headers and footers. Learn more about the available options in the HTML to PDF API — PHP SDK documentation.

Server Side PDF Generation

In this section we will show two common PDF generation scenarios.

Generate PDF and send it to the browser

The following code converts example.com to PDF and sends it as a response:

convertUrl("https://example.com.com/"); header("Content-Type: application/pdf"); header("Cache-Control: no-cache"); header("Accept-Ranges: none"); header("Content-Disposition: inline; filename=\"example.pdf\""); echo $pdf; > catch(\Pdfcrowd\Error $why) < fwrite(STDERR, "Pdfcrowd Error: \n"); > ?>

To convert an HTML string you can use convertString() instead of convertUrl():

Since Content-Disposition is set to inline the generated PDF is opened in the browser. If you change it to attachment the browser will pop up the file download dialog:

Provide a PDF version of your web pages

This example shows how to enhance your PHP code so it can return a PDF version of your web pages. Let’s look at the following helper function:

 1) < unset($_GET["pdf"]); $url = $url . "?" . http_build_query($_GET, '', '&'); >// call the API $client = new \Pdfcrowd\HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d"); $pdf = $client->convertUrl($url); // send the generated pdf to the browser header("Content-Type: application/pdf"); header("Cache-Control: no-cache"); header("Accept-Ranges: none"); header("Content-Disposition: attachment; filename=\"created.pdf\""); echo $pdf; > catch(\Pdfcrowd\Error $why) < fwrite(STDERR, "Pdfcrowd Error: \n"); > return True; > ?>

The generatePDF() function first checks if there is a pdf field in the query string. If yes, then the field is removed from the url. The function then passes the modified url to the API and finally sends the generated PDF to the browser.

You can use the function in your code like this:

https://mydomain.com/page.php?pdf=1 now returns a PDF version of https://mydomain.com/page.php.

Источник

Оцените статью