Giter Site home page Giter Site logo

bhofstaetter / silverstripe-wkhtmltopdf Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 5.0 27 KB

Adds the possibility to create PDFs from all DataObjects with WKHTMLTOPDF

License: BSD 3-Clause "New" or "Revised" License

PHP 78.87% Scheme 8.03% HTML 13.10%

silverstripe-wkhtmltopdf's Introduction

This module is now abandoned, for SilverStripe 4 you can use my new and improved version silverstripe-wkhtmltox


Create PDFs in SilverStripe with the power of WKhtmlTOpdf

This module adds the possibility to simply create PDFs from every DataObject you have. Based on WKhtmlTOpdf and mikehaertl's php wrapper.

Installation

$ composer require creativesynergy/silverstripe-wkhtmltopdf

Getting started

  1. WKhtmlTOpdf must be installed on your server to use this module
  2. You'll need to copy the footer.html and header.html files from the module templates folder to mysite/templates/Pdf/
  3. Create a css file called 'pdf.css' located under themes/your-theme/css/

Usage

Basics

$trainer = Trainer::get()->first();
$pdf = new SS_PDF();
$html = $pdf::getHtml($trainer);
$pdf->add($html);
$pdf->save('trainer.pdf');

Add a cover

$pdf->add('.../path/to/cover.html', 'Cover');     // You could use the same inputs as listed under "Add pages"

Add pages

$pdf->add('<html>...</html>');                    // Html code
$pdf->add('.../path/to/page.html');               // Html file
$pdf->add('https://www.google.com');              // Website
$pdf->add($pdf::getHtml($dataObject));       // DataObject

Add specific options for one page

$options = array(
  'image-quality'     => 100,
  'margin-bottom'     => 0,
  'margin-left'       => 0,
  'margin-right'      => 0,
  'margin-top'        => 0,
  'header-html'       => null,
  'footer-html'       => null
);

$pdf->add('.../path/to/cover.html', 'Cover', $options);

All available options can be found here

Add a page and pass some variables to it

$variables = array(
  'Title'             => 'My New Title',
  'MyFreakyWhatever'  => DataObject::get()->byID(123)->WhatEver()
);

$html = $pdf::getHtml($trainer, $variables);

$pdf->add($html);

Change the page template

$html = $pdf::getHtml($trainer, $variables, 'NewPDFTemplate');
$pdf->add($html);

By default the module will look for a template called "ClassName_pdf"

Preview in browser

$pdf->preview();

Save

$pdf->save('trainer.pdf');                        // This will also return an file instance to work with

Specify the folder to save in

$pdf->setFolderName('trainers/pdfs');

Download

$pdf->download('trainer.pdf');

Global options

Set the global options used for all pages in this pdf

$css = BASE_PATH . '/themes/' . SSViewer::current_theme() . '/css/pdf.css';
$header = BASE_PATH . '/mysite/templates/Pdfs/header.html';
$footer = BASE_PATH . '/mysite/templates/Pdfs/footer.html';

$options = array(
  'enable-javascript',
  'dpi'               => 150,
  'image-dpi'         => 150,
  'image-quality'     => 100,
  'user-style-sheet'  => $css,
  'header-html'       => $header,
  'footer-html'       => $footer,
  ...
  ...
);

$pdf->setGlobalOptions($options);

Set or modifiy only one global option

$pdf->setOption('enable-javascript');

$pdf->setOption('dpi', '300');

$pdf->setOption('run-script', array(
  '../path/to/local/script1.js'
));

$pdf->setOption('replace', array(
  '{whatever}' => 'something new'
));

Remove one global option

$pdf->removeOption('replace');

Templates, Header & Footer and Styling

Page template

If your page ist based on an DataObject and you generate the html with the $pdf::getHtml() function, you'll be able to set a specific template to use for this page by passing it as third parameter to the function (without the .ss ending!). By default, the module will search for a template called "Classname_pdf".

Header & Footer

WKhtmlTOpdf let you specify seperate files for your pdf's header and footer section. By default they are located under mysite/templates/Pdf/header.html and mysite/templates/Pdf/footer.html.

You can change the location of those files or remove the header and/or footer completely by changing the global or page specific options.

$pdf->setOption('header-html', '/path/to/header.html');
$pdf->removeOption('footer-html');

Demo Pdf.ss, header.html and footer.html files are included to get you started

Styling

Thanks to WKhtmlTOpdf you have full CSS3 and HTML5 support and will be able to do fancy things "without" the limitations you'll have to face while using other tools like dompdf or tcpdf. You can even use javascript to modify your pages.

By default the module requires a pdf.css in under themes/your-theme/css/pdf.css You can change this by setting the global or page specific option

$pdf->setOption('user-style-sheet', '/path/to/pdf.css');

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.