Giter Site home page Giter Site logo

silverstripe-wkhtmltopdf's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

silverstripe-wkhtmltopdf's Issues

PDF doesn’t render

Hi
I have installed all the correct addon and binary for the addon.
I followed the instruction to the T, if I echo out the $html all my code is displayed correctly. It produced the pdf in the folder I choose to save it and the title I choose , but it doesn’t produce anything, no pdf and in assets folder it shows no size in file size .
Any ideas would be helpful
Thanks

Template Not Found / Missing SS 4 requirements

A little preamble

Hi @bhofstaetter ,

First of all thanks for your work. Just these last days, I googled around for a module like this one - to use it in my new project - but with a simple approach to adapt it to the last SilverStripe release. Just a few notes.
In order to work on my ongoing project (SS 4.0.3), I corrected some missing elements in the module core, to allow its basic execution (such as namespaces, methods and other little modification).
At the moment, I hardcoded them for an on-the-fly debugging. In the next future, if you decide to integrate them officially, I'll be at your disposition to help you somehow. Here they go:

Namespaces

<?php
use mikehaertl\wkhtmlto\Pdf;
// Namespace definitions
use SilverStripe\View\SSViewer;
use SilverStripe\View\Requirements;
// For a complete setting. we'll need to include the others. 
// Just for a brief-run I included just these two
[...]

Global Options

[...]
  public function setGlobalOptions($options = null) {
    if(!$options) {
      // Get the current theme - SS ^4
      // SSViewer::current_theme is deprecated. It should be present a check to
      // Define it depending by the platform version
      $theme = SSViewer::get_themes(); 
      // Obtaining the first available theme
      $css = BASE_PATH . '/themes/' . reset($theme) . '/css/pdf.css';

The Issue

In my project, the admin needs to download a generated document (PDF) straight from the back-end. So I implemented a custom GridField action button in order to allow it.
On button click, the module should generate the document - as expected - then save it, download it, etc.
I opened this issue in order to investigate this exception:

PHP Warning: None of the following templates could be found: Fattura_pdf in themes "Array
(
[0] => silverstripe/admin:cms-forms
[1] => $default
) " in /Applications/MAMP/htdocs/ss-ecommerce/ss-ecommerce/vendor/silverstripe/framework/src/View/SSViewer.php on line 210

By assuming my preamble, I found what's the cause. Basically, getHtml() looks for a template inside the current "context": in my case, the CMS back-end. So, it searches for a template in the back-end assets folder, not in the template one (as you can see from the exception message). My goal is to achieve the possibility to get the template file - actually inside the main theme folder - without hardcoding/copy/paste it in the framework core (in order to avoid future updates issues and so on). I tried to extend getHtml by adding a path variable, but I'm missing the deep knowledge of SSViewer functioning, in order to achieve a solution like this:

Action/Button

[...]
// $record <- GridField record
// Generazione PDF
                    $pdf = new SS_PDF();
                    // Getting theme - As seen above
                    $tema = SSViewer::get_themes();
                    $path = BASE_PATH .'/themes/'. reset($tema);
                    $opzioni = [
                        'dpi' => 300,
                        'user-style-sheet' => $path .'/css/dist/pdf.css'
                    ];
                    $variabili = [
                        'Title' => 'Test'
                    ];

                    $pdf->removeOption('header-html');
                    $pdf->removeOption('footer-html');
                    $pdf->setGlobalOptions($opzioni);
                    $pdf->add($pdf::getHtml(Fattura::get()->byID($record->ID), $variabili));
                    // DEBUG
                    $pdf->preview();
                    $pdf->setFolderName('ss-ecommerce/fatture/pdf');
                    $pdf->save('test');
                    $pdf->download('test');

                    Controller::curr()->getResponse()->setStatusCode(200, 'PDF downloaded.');
[...]

getHtml()

[...]
// Adding $path parameter
static function getHtml($obj, $variables = null, $template = null, $path = null) {
    Requirements::clear();
    if(!$template) {
      $template = sprintf("%s_pdf", $obj->ClassName);
    }
   // Pass a full path here - not only name - assuming $template as not null
    $viewer = new SSViewer($template, $path); // <-- Desired approach
    $html = $viewer->process($obj, $variables);
    return $html;
[...]

Do you think that we can collaborate in order to reach this approach? This panorama could be the best possible, especially in my specific case. Indeed if you think that there's another simplest way to solve it, it'll be welcome.

Thanks in advance for your support.

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.