public function rootAction(Application $app, Request $request)
 {
     $data = $this->prepareInput();
     if ($data === null) {
         return new JsonResponse(['error' => 'no json data found'], 400);
     }
     $templateName = isset($data['template']) ? $data['template'] : null;
     $templateData = isset($data['data']) ? $data['data'] : null;
     if (!$templateName || !$templateData) {
         return new JsonResponse(['error' => 'template and data must be set'], 400);
     }
     $repo = $app->getTemplateRepository();
     $template = $repo->getByName($templateName);
     if (!$template) {
         return new JsonResponse(['error' => "template {$templateName} not found"], 404);
     }
     $twig = new \Twig_Environment(new \Twig_Loader_String());
     $html = $twig->render($template->getTemplate(), $templateData);
     $file = new File();
     $file->setId(Uuid::uuid4()->toString());
     $file->setCreatedAt(date('Y-m-d H:i:s'));
     $file->setPath($this->getFilePath($file));
     $snappy = new Pdf();
     if (substr(php_uname(), 0, 7) == "Windows") {
         $snappy->setBinary('vendor\\bin\\wkhtmltopdf.exe.bat');
     } else {
         $snappy->setBinary('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');
     }
     $snappy->generateFromHtml($html, $file->getPath());
     $repo = $app->getFileRepository();
     $repo->add($file);
     return new JsonResponse(['id' => $file->getId()], 201);
 }
 public function test()
 {
     $bin = BASE_PATH . 'vendor/bin/wkhtmltopdf-amd64';
     // Display the resulting pdf in the browser
     // by setting the Content-type header to pdf
     $snappy = new Pdf();
     $snappy->setBinary($bin);
     $snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', BASE_PATH . 'storage/bill-123.pdf');
 }
 /**
  * Generates the pdf with Snappy
  *
  * @param string $content
  * @param string $path
  *
  * @return string
  */
 protected function doPdf($content, $path, $io)
 {
     $snappy = new Pdf();
     //@TODO: catch exception if binary path doesn't exist!
     $snappy->setBinary($this->wkhtmltopdfPath);
     $snappy->setOption('orientation', "Landscape");
     $snappy->generateFromHtml($content, "/" . $path . 'dc-cheatsheet.pdf');
     $io->success("cheatsheet generated at /" . $path . "/dc-cheatsheet.pdf");
     // command execution ends here
 }
Example #4
0
<?php

use Knp\Snappy\Pdf;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Seld\JsonLint\JsonParser;
use Symfony\Component\PropertyAccess\PropertyAccess;
//Request::setTrustedProxies(array('127.0.0.1'));
$app->post('/', function (Request $request) use($app) {
    $parser = new JsonParser();
    $accessor = PropertyAccess::createPropertyAccessor();
    $snappy = new Pdf();
    $snappy->setBinary($app['wkhtmltopdf.binary']);
    $parameters = $parser->parse($request->getContent());
    if ($accessor->isReadable($parameters, 'options')) {
        foreach ((array) $accessor->getValue($parameters, 'options') as $name => $value) {
            $snappy->setOption($name, $value);
        }
    }
    $app['tmpFile'] = sys_get_temp_dir() . '/' . md5($request->getContent());
    if ($accessor->isReadable($parameters, 'source.url')) {
        $dns = new Net_DNS2_Resolver();
        $dns->query(parse_url($accessor->getValue($parameters, 'source.url'), PHP_URL_HOST));
        $snappy->generate($accessor->getValue($parameters, 'source.url'), $app['tmpFile'], [], true);
    } elseif ($accessor->isReadable($parameters, 'source.html')) {
        $snappy->generateFromHtml($accessor->getValue($parameters, 'source.html'), $app['tmpFile'], [], true);
    } elseif ($accessor->isReadable($parameters, 'source.base64')) {
        $snappy->generateFromHtml(base64_decode($accessor->getValue($parameters, 'source.base64')), $app['tmpFile'], [], true);
    }
    return new BinaryFileResponse($app['tmpFile']);
Example #5
0
<?php

require_once "vendor/autoload.php";
use Knp\Snappy\Pdf;
if (isset($_POST) && isset($_POST["envoyer"])) {
    $myProjectDirectory = '/var/www/html/mouskipd';
    $snappy = new Pdf();
    $snappy->setBinary('/var/www/html/mouskipd/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');
    $snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/var/www/html/mouskipd/tmp/bill-123.pdf');
    var_dump($snappy);
    echo "<pre>";
    var_dump($snappy);
    echo "</pre>";
}
?>

<form action="index.php" method="POST" accept-charset="utf-8">
    <input type="submit" name="envoyer">
</form>
 public function newPDF()
 {
     $snappy = new Pdf();
     $snappy->setBinary('/usr/local/bin/wkhtmltopdf');
     $competences = $app['dao.competence']->findAll();
     return $app['twig']->render('competence.html.twig', array('competences' => $competences));
 }
Example #7
-1
 /**
  * {@inheritDoc}
  */
 public function generate($path, $html)
 {
     $ext = pathinfo($path, PATHINFO_EXTENSION);
     if (null === $ext) {
         $path .= ".ext";
     } elseif ("pdf" !== $ext) {
         throw new InvalidArgumentException(sprintf("Your destination path must have a .pdf extension for conversion, '%s' passed.", $ext));
     }
     $pdf = new Pdf();
     $pdf->setBinary($this->_getBinDir() . 'wkhtmltopdf-' . $this->_getBinaryType());
     foreach ($this->_options as $key => $value) {
         $pdf->setOption($key, $value);
     }
     if (is_file($path)) {
         unlink($path);
     }
     // Strip out `@font-face{ ... }` style tags as these break the pdf
     $html = preg_replace('/@font-face{([^}]*)}/ms', '', $html);
     $pdf->generateFromHTML($html, $path);
     return new File($path);
 }