Giter VIP home page Giter VIP logo

dompdfmodule's Introduction

DOMPDFModule

Build Status Code Climate Test Coverage Total Downloads License

The DOMPDF module integrates the DOMPDF library with Zend Framework 2 with minimal effort on the consumer's end.

Requirements

Installation

Installation of DOMPDFModule uses PHP Composer. For more information about PHP Composer, please visit the official PHP Composer site.

Installation steps

  1. cd my/project/directory

  2. create a composer.json file with following contents:

    {
        "require": {
            "dino/dompdf-module": "dev-master"
        }
    }
  3. install PHP Composer via curl -s http://getcomposer.org/installer | php (on windows, download http://getcomposer.org/installer and execute it with PHP)

  4. run php composer.phar install

  5. open my/project/directory/config/application.config.php and add the following key to your modules:

    'DOMPDFModule',

Configuration options

You can override options via the dompdf_module key in your local or global config files. See DOMPDFModule/config/module.config.php for config options.

Usage

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use DOMPDFModule\View\Model\PdfModel;

class ReportController extends AbstractActionController
{
    public function monthlyReportPdfAction()
    {
        $pdf = new PdfModel();
        $pdf->setOption('fileName', 'monthly-report');            // "pdf" extension is automatically appended
        $pdf->setOption('display', PdfModel::DISPLAY_ATTACHMENT); // Triggers browser to prompt "save as" dialog
        $pdf->setOption('paperSize', 'a4');                       // Defaults to "8x11"
        $pdf->setOption('paperOrientation', 'landscape');         // Defaults to "portrait"
        
        // To set view variables
        $pdf->setVariables(array(
          'message' => 'Hello'
        ));
        
        return $pdf;
    }
}

Development

So you want to contribute? Fantastic! Don't worry, it's easy. Local builds, tests, and code quality checks can be executed using Docker.

Quick Start

  1. Install Docker CE.
  2. Run the following from your terminal:
docker build -t dino/dompdf-module .
docker run -v composer-cache:/var/lib/composer -v ${PWD}:/opt/app dino/dompdf-module

Super easy, right? Here's a quick walk through as to what's going on.

  • docker build -t dino/dompdf-module . builds a docker image that will be used for each run (i.e. each time docker run is executed) and tags it with the name dino/dompdf-module.
  • docker run -v composer-cache:/var/lib/composer -v ${PWD}:/opt/app dino/dompdf-module runs the default build in a new Docker container derived from the image tagged dino/dompdf-module. The root of the project and PHP Composer cache volume are mounted so that artifacts generated during the build process are available to you on your local machine.

Note: You only need to run the first command once in order to build the image. The second command is what executes the build (build, tests, code quality checks, etc.).

Other Supported PHP Versions

By default, builds executed using Docker are done so using the latest stable version of PHP. If you're adventurous you can execute builds against other supported versions of PHP.

PHP 5.6

docker build --build-arg PHP_VERSION=5.6 --tag dino/dompdf-module-php56 .
docker run -v composer-cache:/var/lib/composer -v ${PWD}:/opt/app dino/dompdf-module-php56

To-do

  • Add command line support.

dompdfmodule's People

Contributors

brunospy avatar ddetyuk avatar jamiehannaford avatar manuakasam avatar raykolbe 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dompdfmodule's Issues

Add CLI support

Command line should support functions of:

  • dompdf.php
  • load_font.php

cant install via composer

dev:master
and
v0.3.1

both have deps for different commits on dev master that cannot be satisfied,
checking out the "hotfix 18" branch doesnt make much sense either, as after new changes are made, they wont be sitting in that branch.

Configure DomPdf instantiation with service

Now the DomPdf class is instantiated directly (see https://github.com/raykolbe/DOMPDFModule/blob/master/src/DOMPDFModule/View/Renderer/PdfRenderer.php#L38). But for example we always use DomPdf with these configuration options:

$domPdf = new DomPdf();
$domPdf->load_html(utf8_decode($html));
$domPdf->set_paper('a4', 'portrait');
$domPdf->set_base_path(realpath($path));
$domPdf->render();

If you configure the DomPdf as a service, I can overwrite the service with these properties and then be more flexible in my pdf rendering.

Use spaces instead of tabs (PSR-1)

The layout on GitHub is a bit weird when there's a mixture between tabs and spaces. You might want to apply PSR-1 as that's a new adopted standard for big php projects dictating a 4-space indention rule.

Secondly, the new PSR-1 uses a use statement on every line, so no comma separated list.

Class 'DOMPDFModule\View\Model\PdfModel' not found

in my controller
use DOMPDFModule\View\Model\PdfModel;

public function monthlyReportPdfAction()
{
$model = new PdfModel();
$model->setOption('paperSize', 'a4');
$model->setOption('paperOrientation', 'landscape');

    return $model;
}

clear my issue,
I just download ur DOMPDFModule and put in my vendor

External CSS and @media print

Hello Ray,

First of all, good work!

I have some questions: Is it possible to use external style sheets? I am using ZF2 and was trying to set a basePath to use my stylesheets, but it doesn´t work. My stylesheets which I am using (embedded in an extra layout.phtml) do not show up or seem to have an effect on the pdf.

What should be exactly in the basePath? While having a normal Zend Framework 2 Skeleton Application? I tried it with $model->setOption('basePath', '/public/'), as my path in the layout.phtml is ...->appendStylesheet($this->basePath() . '/css/main.css'), but it is not working.

And another issue, is it possible to use @media print with the DOMPDFModule? I tried it inline with @media print { h1 { color: indianred; }}, but no success. It only takes affect when saving the pdf manually via the browser ("Print"/...), but not in the generated PDF from the module.

Thanks!

zend-console dependency breaks existing code

Hi, we have this dependency as a legacy piece of code, (with a require on dev-master) but today when I performed a composer update the dependency tree broke as you require zend-console 2.5.0 when we already have installed and is required by other vendors for zend-console 2.6.0. Is there a hard dependency on 2.5.0 or can this be set to 2.6.0?

Call to undefined method Zend\Mvc\View\Http\ViewManager::getResolver()

Fatal error: Call to undefined method Zend\Mvc\View\Http\ViewManager::getResolver() in /home/marcio/vhosts/vendor/dino/dompdf-module/src/DOMPDFModule/Mvc/Service/ViewPdfRendererFactory.php on line 39

@raykolbe since version 2.7.0 of zend-mvc component, the ViewManager no longer has the methods getResolver() and getRenderer(). The ViewResolver and ViewRenderer now must be instanciated by factories. I sent a pull request #36 to solve this. I hope that helps.

Setting a layout

Hey there!
What about setting a layout for the view. We use twitter bootstrap and the output of the pdf should be styled in bootstrap styles. Is there a way to set a layout for the pdf-view or do we have to input the styles directly in the pdf-view?

$this->layout('foo/bar');

doesnt work

Add support for other rendering engines

Currently templates only support being rendered by the built-in PhpRenderer object. It would be nice to add support for other rendering engines (e.g. Twig).

Need a new tag

Please could you generate a new tag? That´s because all the versions point to the dompdf/dompdf, dev-master. And The new dompdf/dompdf don´t has anymore the include folder. So when you "compose update" the project it gets an error in your /DOMPDFModule/Service/DOMPDFFactory.php Line 111. If you fix the dompdf/dompdf to the version 0.6.2 this error you be resolved.

How to generate pdf files on server side

Hi,

maybe somebody besides me is searching for a solution on how to generate pdf files on server side. Here is a small example that works for me. Maybe it finds its way to the readme. :)

$pdfView = new ViewModel();
# When the action can be found at:
# MyModule/MyController/mypdf
# the path to the according template would be:
# my-module/my-controller/mypdf
$pdfView->setTemplate('my-module/my-controller/mypdf');
$pdfView->setVariables(array(
    'somevar' => $somevar,
));
$pdfRenderer = $this->getServiceLocator()->get('ViewPdfRenderer');
$html = $pdfRenderer->getHtmlRenderer()->render($pdfView);
$pdfEngine = $pdfRenderer->getEngine();

$pdfEngine->load_html($html);
$pdfEngine->render();
$pdfContent = $pdfEngine->output();

# make sure to create public/pdf before
file_put_contents(getcwd().'/public/pdf/mypdf.pdf', $pdfContent);

greetings
Andi

P.S.: This is more an enhancement for the readme than a real issue, but I don't know how to change the issue type on github, yet.

Can't include project images or css

I ca't find the way to include site images and css in my pdf

On my controller:

$pdf = new PdfModel();

$pdf->setOption('filename', 'myfile.pdf');
$pdf->setOption('terminal',true);
$pdf->setOption('paperSize', 'a4');

$pdf->setTemplate('my/model/view.phtml');

$pdf->setVariables(array(
    'foo' => $bar,
));
return $pdf;

On my View:

<head>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">
</head>
<body>
   <div class="hero">
      <h3>My pdf file</h3>
      <img src="/img/logo.png" alt="My Logo">
   </div>
</body>

but no image or any css style is displayed.... What I am doing wrong? Please give me a hint. Thanks!

Print Page Numbers

How to print the page numbers of pdf file like "currentPage/totalPage" some where in the document ?

Options for setOptions?

Hello, i got a question.
Which commands has the setOption function? I want to pass it a html file generated by a function on my zf2 module. I'm very noob in zf2, sorry for that.

Thanks a lot!! And thanks for the module!

Getting an error while generatin pdf.

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode,

This was the error come when something is missed in html. So, in the place of this error we can show an error message .

Output is not aligned properly

Hello,

I'm trying to align table fields under the appropriate table header but for some reason it is not lining up correctly.

I've included a screenshot of the actual PDF document that is rendered.

Thanks!

vdhlv

DOMPdf wouldn't work on my Linux box

Hi Reymond,

DOMPDF works fine on my Windows machine (WAMP). Everything's ok.

However, after moving my code to Linux server (debian, nginx, php5-fpm) theres no error but page is not rendered as PDF. I see all elements, without error, but as HTML page, also CSS is not processed. Everything same on WAMP working fine. I have cheked and GD, DOM, MBString extensions everything is enabled.

I dont know how to debug since there's no error. Please take a look at
http://www.carskiducan.com/korpa/prikaziPDF/c710daf3d2e69f9e33b57c204f49ac5bdc035855 it should be rendered as pdf but it isn't. Any help appreciated.

Regards

Create a tag @ #a2e2e15

Can you please create a tag at this commit #a2e2e154ffc84c724ba6a2c1ae63a948d8fc0e1d

As this was the last version before the minimum dompdf version constraint was increased from ^0.6.0 to ^0.8.0 in your composer.json as my code is not compatible with the newer version of dompdf, so i can not use dev-master.

Saving the PDF

Hey Ray,

is there a way to save the generated PDF-File?
I am talking about saving it on server-side to not re-render already generated PDF Files that have not been changed (or for versioning control as an example).

I assume i have to attach a listener to the renderer.post event. Have you ever done this? I assume this would be a highly requested feature for people working with the Module.

IF you have done this it would be awesome if you could provide the code for such a thing. I'll be trying to figure it out for myself though ;-)

How To: Get DOMPDFModule working with ZF3

I've made the module working on ZF3. But actually only with dompdf version 0.6.2. You should totally upgrade to newer version of it!

But so far, just to give you an hint what to do to make this module working on ZF3, I made an change list while updateing it. It works pretty well then. Maybe helpful for others too, so I will let this here.

It really easy to do, just follow this easy steps ...


  1. Delete modules/DOMPDFModule/Module.php

  2. Move files from modules/DOMPDFModule/src/DOMPDFModule/
    to modules/DOMPDFModule/src/ (one directory up)

  3. Change the path in getConfig() function
    in modules/DOMPDFModule/src/Module.php
    to __DIR__ . '/../config/module.config.php

  4. Remove getAutoloaderConfig function
    from modules/DOMPDFModule/src/Module.php

  5. Change from use Zend\ServiceManager\FactoryInterface;
    to use Zend\ServiceManager\Factory\FactoryInterface; in:

  • modules/DOMPDFModule/src/MVC/Service/ViewPdfRendererFactory.php
  • modules/DOMPDFModule/src/MVC/Service/ViewPdfStrategyFactory.php
  • modules/DOMPDFModule/src/Service/DOMPDFFactory.php
  1. Change function public function createService(ServiceLocatorInterface $serviceLocator)
    to public function __invoke(ContainerInterface $container, $requestedName, array $options = null) in:
  • modules/DOMPDFModule/src/MVC/Service/ViewPdfRendererFactory.php
  • modules/DOMPDFModule/src/MVC/Service/ViewPdfStrategyFactory.php
  • modules/DOMPDFModule/src/Service/DOMPDFFactory.php
  1. Add new imports:
  • use Interop\Container\ContainerInterface;
  • use Interop\Container\Exception\ContainerException;
  • use Zend\ServiceManager\Exception\ServiceNotCreatedException;
  • use Zend\ServiceManager\Exception\ServiceNotFoundException;
  • use Zend\ServiceManager\ServiceManager;

to the following file headers:

  • modules/DOMPDFModule/src/MVC/Service/ViewPdfRendererFactory.php
  • modules/DOMPDFModule/src/MVC/Service/ViewPdfStrategyFactory.php
  • modules/DOMPDFModule/src/Service/DOMPDFFactory.php
  1. Instead of $serviceLocator->get
    use $container->get(ServiceManager::class)->get
    in factories

  2. Change defined('DOMPDF_DIR') || define("DOMPDF_DIR", __DIR__ . '/../../../../../dompdf/dompdf');
    to defined('DOMPDF_DIR') || define("DOMPDF_DIR", __DIR__ . '/../../../../vendor/dompdf/dompdf');
    in modules/DOMPDFModule/src/Service/DOMPDFFactory.php

  3. Change require_once __DIR__ . '/../../../config/module.compat.php';
    to require_once __DIR__ . '/../../config/module.compat.php';
    in modules/DOMPDFModule/src/Service/DOMPDFFactory.php

  4. Change $viewManager->getResolver()
    to $serviceLocator->get("ViewResolver")
    in modules/DOMPDFModule/src/Mvc/Service/ViewPdfRendererFactory.php

  5. Change $viewManager->getRenderer()
    to $serviceLocator->get("ViewRenderer")
    in modules/DOMPDFModule/src/Mvc/Service/ViewPdfRendererFactory.php

  6. Add autoloader configuration to composer.json:

"autoload": {
  "psr-4": {
    "DOMPDFModule\\": "module/DOMPDFModule/src/"
  }
}
  1. Run composer command dump-autoload to rebuild autoloading:
$ composer dump-autoload

This tutorial assumes that dompdf is installed in version 0.6.2 by composer in vendor directory.

setting filename does not trigger download

Hi Raymond,

Almost everything works smoothly for me, except:
You say somewhere that setting the filename will trigger download. I have set:
$model->setOption('fileName', 'voucher');

which should get me a download to voucher.pdf, but instead the pdf is opened in the browser window and when I save it from the acrobat plugin it has the name document.pdf

Am I doing something wrong, or have I stumbled upon something unexpected?

Thanks in advance, Raymond:-)

Manual setup guide

Hi Ray,

I do not use Composer at the moment and I had tried to download your DOMPDFModule as well as the dompdf package from the DOMPDF site. Could you describe what else needs to be done besides the following in order to get it working?:

  1. Putting them into the vendor directory:
    vendor/dino/DOMPDFModule
    vendor/dompdf/dompdf

  2. Adding entry into the init_autoloader.php file:
    Zend\Loader\AutoloaderFactory::factory(array(
    'Zend\Loader\StandardAutoloader' => array(
    'autoregister_zf' => true,
    'namespace' => array(
    'dompdf' => 'vendor/dompdf/dompdf',
    'DOMPDFModule' => 'vendor/dino/DOMPDFModule',
    )
    )
    ));

  3. Adding entry into the autoload_classmap.php file:

return array(
'dompdf' => DIR . '/../../vendor/dompdf/dompdf/',
'DOMPDFModule' =>DIR.'/../../vendor/dino/DOMPDFModule/',//=>DIR.'/../../vendor/DOMPDFModule-master/',
);

I tried render and putting a PDF file in a directory but received the warning that the PdfModel module could not be found:
Class 'DOMPDFModule\View\Model\PdfModel' not found.

Problems on installation

My composer.json:

"require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.2.*",

    "dino/dompdf-module": "dev-master"
}

Command executed:
$ php composer.phar update

Problem 1
   - Installation request for dino/dompdf-module dev-master -> satisfiable by dino/dompdf-module[dev-master].
   - dino/dompdf-module dev-master requires dompdf/dompdf dev-master#1abb9117a50c2854761bacaaff4b8a08257a1e8e -> no matching package found.

Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
  see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.

Take this opportunity to ask that the installation of the module dino/dompdf not depend on git. I don't use git on my environment, and only composer should be enough.

Thanks!
Waiting your answer to keep using this excellent module! :)

Does this work with ZF3?

Hello,
Will this work for ZF3? I received a exception for zf3.
For ZF2 its an awesome module to use.
Can you please inform me if it has been tested for zf3 before?

Thanks in advance

PHP7 compatibility (update composer to dompdf 0.7.0)?

Hello,

I was testing the PHP7 compatibility, but it fails in current version of dompdf used. The creators of the dompdf have claimed it was fixed in the version 0.7.0 but I am not able to force the composer to use that file.

Is there more to be done to achieve PHP7 compatible code?

Create test for child models

Currently this has not been considered as a feature. What happens when we have child models in a PdfModel? Should this be supported?

class DOMPDF not found

Hi. I'm trying to use this module but I have a error :
Fatal error: Class 'DOMPDF' not found in C:\xampp\htdocs\antimicrobiano\vendor\dino\DOMPDFModule\src\DOMPDFModule\View\Renderer\PdfRenderer.php on line 77.
I installed dompdf via composer but dompdfmodule manually because composer couldn't install.

Example code

Hi Ray,

Thanks for your work on this.

However in the example code should there not be a setTemplate command just before returning $pdf ?

$pdf->setTemplate();

I have just added this and it works

regds

enable_remote

How I can modify the default value of the file config/module.config.php to set enable_remote to true?

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.