bhofstaetter / silverstripe-wkhtmltopdf Goto Github PK
View Code? Open in Web Editor NEWAdds the possibility to create PDFs from all DataObjects with WKHTMLTOPDF
License: BSD 3-Clause "New" or "Revised" License
Adds the possibility to create PDFs from all DataObjects with WKHTMLTOPDF
License: BSD 3-Clause "New" or "Revised" License
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
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';
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.