Coder Social home page Coder Social logo

html-to-pdf's Introduction

HtmlToPdf

Facade to convert html into pdf, uses the library mpdf

Install

composer require dimns/html-to-pdf

Methods

public HtmlToPdf::__construct ( string $path_to_folder [, array $mpdf_config ] )
  • $path_to_folder (string) Required - Path to the folder where the file will be placed

  • $mpdf_config (array) Optional - Redefining Mpdf settings

    Default settings:

    [
        'format'              => 'A4',
        'orientation'         => 'portrait',
        'default_font'        => 'arial',
        'setAutoTopMargin'    => 'stretch',
        'setAutoBottomMargin' => 'stretch',
    ]
public HtmlToPdf::create ( string $html_body [, string $html_header = null [, string $html_footer = null ]] ) : string
  • $html_body (string) Required - Content
  • $html_header (string) Optional - Header
  • $html_footer (string) Optional - Footer

Usage

Simple

Simple usage

<?php
use DimNS\HtmlToPdf\HtmlToPdf;
use Mpdf\MpdfException;

require_once 'vendor/autoload.php';

$htp = new HtmlToPdf(__DIR__);

$html_body = <<<HTML
<h1>
    Test html to pdf
</h1>
<p>
    <strong>Test</strong> <i>string</i> with <a href="https://domain.tld">link</a>
</p>
HTML;

try {
    echo $htp->create($html_body);
} catch (MpdfException $e) {
    echo $e->getMessage();
}

Advanced

Advanced usage

<?php
use DimNS\HtmlToPdf\HtmlToPdf;
use Mpdf\MpdfException;

require_once 'vendor/autoload.php';

$htp = new HtmlToPdf(__DIR__, [
    'format'         => 'A6',
    'orientation'    => 'landscape',
    'defaultCssFile' => '/path/to/style.css',
]);

$html_body = <<<HTML
<h1>
    Test html to pdf with header and footer
</h1>
<p>
    <strong>Test</strong> <i>string</i> with <a href="https://domain.tld">link</a>
</p>
HTML;

$html_header = <<<HTML
<p>
    <strong>This is header</strong>
</p>
<hr>
HTML;

$html_footer = <<<HTML
<hr>
<p>
    <strong>This is footer</strong>
</p>
HTML;

try {
    echo $htp->create($html_body, $html_header, $html_footer);
} catch (MpdfException $e) {
    echo $e->getMessage();
}

* style.css

.table {
    width: 100%;
}

.text-left {
    text-align: left;
}

.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

h1 {
    font-size: 16pt;
    color: green;
}

html-to-pdf's People

Contributors

dimns avatar mend-bolt-for-github[bot] avatar

Watchers

 avatar

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.