KnpLabs / snappy捆绑包(wkhtmltopdf)无法呈现Chart.js图表

我尝试使用PDF库生成的图表生成Chart.js 但javascript根本无法渲染。

您看到我错过的东西吗?

   # config/packages/knp_snappy.yaml
    knp_snappy:
        pdf:
            enabled:    true
            binary:     '%env(WKHTMLTOPDF_PATH)%'
            options:
              debug-javascript: true
              enable-javascript: true
              javascript-delay: 200
              no-stop-slow-scripts: true

        image:
            enabled:    true
            binary:     '%env(WKHTMLTOIMAGE_PATH)%'
            options:    []

PHP Symfony部分

    /**
     * Download pdf.
     *
     * @param string $slug
     * @param string $language
     *
     * @return Response
     *
     * @throws \Psr\Cache\CacheException
     * @throws \Psr\Cache\InvalidArgumentException
     */
    public function __invoke(string $slug,string $language): Response
    {
            $this->pdfFilesService->setPdflanguage($language);
            $base64Images = $this->pdfFilesService->getBase64Images($slug);
            $profile      = $this->profileData->execute($slug,$language);
            $filename     = $profile['name'].'-report.pdf';
            $path         = $this->parameterBag->get('app.private_pdf_storage').'/'.$filename;

            $html = $this->templating->render('download_pdf.twig',\array_merge($base64Images,[
                'profile'     => $profile,'language'    => $language,]));


            $this->pdf->generateFromHtml([$html],$path,[],true);

            return new Response(
                $path,200,[
                    'Content-Type'        => 'application/pdf','Content-Disposition' => 'inline; filename="'.$path.'"',]
            );

    }

树枝

   <section>
       <div id="barMulti"></div>
   </section>
   <script type="text/javascript" src="{{ chartJs }}"></script>
   <script type="text/javascript">
      function getDataMulti(type) {
         return {
            // The type of chart we want to create
            type,// The data for our dataset
            data: {
               labels: [ ... ],datasets: [
                  {
                     backgroundColor: "#F4F7F9",data: [3,7,4,5,2,6,8,9,7]
                  },{
                     backgroundColor: "#66C4E0",{
                     backgroundColor: "#009DCD",]
            },// Configuration options go here
            options: {
               legend: {
                  display: false,},animation: {
                  duration: 0
               },scales: {
                  yAxes: [{
                     gridLines: {
                        color: "#454D57",ticks: {
                        padding: 20,fontStyle: 'bold',fontColor: '#F4F7F9',min: 0,max: 100,}
                  }],xAxes: [
                     {
                        gridLines: {
                           color: "#454D57"
                        },ticks: {
                           fontStyle: 'bold',padding: 20,callback: function(label) {
                              if (/\s/.test(label)) {
                                 return label.split(" ");
                              }

                              return label;
                           }
                        }
                     }
                  ]
               }
            }
         }
      }

      // var barMulti = document.getElementById('barMulti');
      var barMulti = document.getElementById('barMulti');
      new Chart(barMulti,getDataMulti('bar'));
   </script>
kobehu2000 回答:KnpLabs / snappy捆绑包(wkhtmltopdf)无法呈现Chart.js图表

  

import numpy as np import pandas as pd A=[] B=[] df1=pd.read_csv('numpy.csv',sep=";") for x in range(len(df1.A)): A.append(df1.A[x].split(',')) for x in range(len(df1.B)): B.append(df1.B[x].split(',')) A=np.array(A).astype(np.int) B=np.array(B).astype(np.int) A #array([[1,1,2,2],# [6,7,3,7],# [1,8,5,3]]) B Out[251]: #array([[3,4,4],# [3,5],5]]) 并不是一个完美的库,但它是开源免费   要使用,因此我们需要感谢并尝试帮助其使用或   通过贡献来改善。

wkhtmltopdf GitHub Repo

第一步,您需要测试HTML / TWIG模板是否可以运行Javascript。

wkhtmltopdf

如果这不起作用,请检查您的<script> document.body.innerHTML = "Testing JavaScript PDF Rendering" </script> 配置

wkhtmltopdf

确定您的JavaScript在 # config/packages/knp_snappy.yaml knp_snappy: pdf: enabled: true binary: '%env(WKHTMLTOPDF_PATH)%' options: debug-javascript: true enable-javascript: true javascript-delay: 1500 no-stop-slow-scripts: true 中可用

最重要的事情。。您需要用wkhtmltopdf canvas

来包装styled元素
div

并在 <div class="reportGraph"> <canvas id="barMulti"></canvas> </div> head中放置样式类

css

或者可能是通过内嵌CSS样式化画布容器

然后在包含 .reportGraph { width:850px } 库之后添加此脚本文件

chart.js

然后添加另一个<script> // wkhtmltopdf 0.12.5 crash fix. // https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3242#issuecomment-518099192 'use strict'; (function(setLineDash) { CanvasRenderingContext2D.prototype.setLineDash = function() { if(!arguments[0].length){ arguments[0] = [1,0]; } // Now,call the original method return setLineDash.apply(this,arguments); }; })(CanvasRenderingContext2D.prototype.setLineDash); Function.prototype.bind = Function.prototype.bind || function (thisp) { var fn = this; return function () { return fn.apply(thisp,arguments); }; }; </script> 标签,您将在其中呈现图表。

script
本文链接:https://www.f2er.com/3079883.html

大家都在问