GithubHelp home page GithubHelp logo

ilpaijin / h2p Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kriansa/h2p

0.0 2.0 0.0 175 KB

H2P - Convert HTML to PDF using PHP and PhantomJS.

Home Page: http://garajau.com.br/blog/2013/03/h2p-convert-html-files-to-pdf/

h2p's Introduction

H2P Build Status

Convert your HTML files to PDF with ease. Thanks to PhantomJS, you can write CSS3, HTML5 and Javascript and it will convert and print your files just like you see them in your browser.

How to use

It's very simple, but first you must download the PhantomJS binary file according to your system (Windows, Mac, Linux-X86 or Linux-X86_64) and put it in the right bin folder.

You can download it here: http://phantomjs.org/download.html

Getting started

use H2P\Converter\PhantomJS;

$converter = new PhantomJS();

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert('http://www.google.com/', '/home/user/Documents/page.pdf');

If you want to convert a HTML string, do it like so:

use H2P\Converter\PhantomJS;
use H2P\TempFile;

$converter = new PhantomJS();
$input = new TempFile('<b>test string to pdf</b>', 'html'); // Make sure the 2nd parameter is 'html'

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert($input, '/home/user/Documents/page.pdf');

Advanced usage

use H2P\Converter\PhantomJS;
use H2P\TempFile;
use H2P\Request;
use H2P\Request\Cookie;

$converter = new PhantomJS(array(
    // You should use 'search_paths' when you want to point the phantomjs binary to somewhere else
    // 'search_paths' => shell_exec('which phantomjs'),
    'orientation' => PhantomJS::ORIENTATION_LANDSCAPE,
    'format' => PhantomJS::FORMAT_A4,
    'zoomFactor' => 2,
    'border' => '1cm',
    'header' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
    'footer' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
));

// Create a full custom request
$request = new Request(
    'http://www.google.com/',
    Request::METHOD_POST,
    array('param' => 'value'), // POST params
    array('X-Header' => 'value'), // Custom headers
    array(
        new Cookie('Cookie', 'value', 'domain'), // Create a basic cookie
    )
);

$destination = new TempFile();
$converter->convert($request, $destination);

You can find more examples in the samples folder.

Composer

Just put { "kriansa/h2p": "dev-master" } into your require property.

License

  • MIT License

h2p's People

Contributors

kriansa avatar vjnrv avatar

Watchers

 avatar  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.