修复snappy生成的pdf报告页数

时间:2017-08-15 11:18:15

标签: symfony pdf twig snappy

我正在使用snappy生成pdf报告,

Pdf包含文本和图表,每页应包含4页,其中包含与特定主题相关的数据。

所以我所做的就是创建宽度为4的div:100%和高度:100%而不是4页我有9页

问题出在哪里?我甚至尝试使用'page-size'=>'A4',但生成相同的9页

public function pdfAction(Survey $survey)
{
    $chart=new ChartController();
    $event = $survey->getEvent();


    $year = $survey->getYear();
    $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
    $qb->select('e')
        ->from('AppBundle:Survey', 'e')
        ->where('e.year <= :year')
        ->andWhere('e.event = :event')
        ->setParameter('year', $year)
        ->setParameter('event', $event->getId())
        ->orderBy('e.year', 'DESC')
        ->setMaxResults(4);
    $survey_year= =  $qb->getQuery()->getResult();
    $date = array();

    $data1  = array();
    $data2  = array();
    $data3  = array();
    $data4  = array();


    $survey_year= array_reverse($survey_year, true);
    foreach($survey_year as $ea){
        $date[] = $ea->getAnnee();
        $data1[] = $ea->getForm()->getBudget ();
        $data2[] = $ea->getForm()->getRessources();
        $data3[] = $ea->getForm()->getTauxGlobal();
        $data4[] = $ea->getForm()->getTauxConsumption();


    }


    $list1= array(
        'name' => 'Budget',
        'type'  => 'column',
        'color' => '#4572A7',
        'yAxis' => 1,
        'dataLabels' => array(
            'enabled' => true
        ),
        'data' => $data1
    );


    $list2 =  array(
        'name' => 'Ressource',
        'type'  => 'column',
        'color' => '#D49EDA',
        'yAxis' => 1,
        'dataLabels' => array(
            'enabled' => true
        ),
        'data' => $data2
    );


    $list3= array(
        'name' => 'Taux  global',
        'type'  => 'spline',
        'color' => '#0A0F19',
        'dataLabels' => array(
            'enabled' => true
        ),
        'data' => $data3
    );


    $list4 =  array(
        'name'=> 'Taux Consumption',
        'type'  => 'spline',
        'color' => '#225824',
        'dataLabels' => array(
            'enabled' => true
        ),
        'data' => $data4
    );

    $containerpie1 = $chart->pieChart1($survey);

    $containerpie2 = $chart->pieChart2($survey);
    $containerpie3=$chart->pieChart3($survey);
    $container4=$chart->multiAxeChart($date, $list1, $list2,$list3,$list4);



    $html =  $this->render('AppBundle:event:pdf.html.twig',array(
        'survey' =>$survey,
        'containerpie1'=>$containerpie1,
        'containerpie2'=>$containerpie2,
        'containerpie3'=>$containerpie3,
        'container4' => $container4,

    ));




    return new Response(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html,array(

            'default-header'=>false,
            'encoding' => 'utf-8',
            'images' => true,
            'enable-javascript' => true,
            'javascript-delay' => 5000
        )),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="file1.pdf"'
        )
    );

}

Pdf.twig.html

<div style="width: 100%; height: 100% ">

    <header>

        <span>{{ survey.event.name </span>
    </header>
<h2 style="width:100% ;background-color:darkseagreen">Genaral</h2>

    <p>
        <span class="address" style="font-weight: bold">address:</span>
        <span >survey.event. address}}</span>
    </p>
    <p>
        <span class="email" style="font-weight: bold">email:</span>
        <span class=survey.event.email}}</span>
    </p>
</div>

<div style="width: 100%;height: 100% ">

        <h2 style="width:100%;background-color:darkseagreen"> Resources </h2>
        <p></p>
        <p>Budget p>



        <div style="display: inline;padding-top: 5em">
            <div id="containerpie1" style="display:inline-block; width: 350px; height: 300px; margin: 0 auto; padding-left: 18em">

            </div>
        </div>
        <div style="display: inline;padding-top: 5em">
            <div id="containerpie2" style="display:inline-block; width: 350px; height: 300px; margin: 0 auto; padding-left: 18em">

            </div>
        </div>
    </div>

<div style="width: 100%;height: 100% ">


        <h2 style="width:100%;background-color:darkseagreen">Activity</h2>
        <p></p>

    <div  >

        <div id="container3" style="display:inline-block;width: 350px;  height: 280px; margin: 0 auto"></div>


    </div>

</div>

<div style="width:100%;height:100% ">


        <h2 style="width:100%;background-color:darkseagreen">Team</h2>
        <p></p>
        <p>Performance</p>
    <div >
        <div id=" container4" style="display:inline-block; width: 800px;  height: 400px; margin: 0 auto">
        </div>
    </div>


</div>

更新

经过多次搜索后,我发现我应该用mm确定高度

<div style="width: 100%; height: 340mm ">

这是唯一的解决方案吗?

1 个答案:

答案 0 :(得分:0)

尝试将onepage类添加到div并添加此CSS:

.onepage {
    page-break-inside: avoid !important;
    page-break-after: always !important;
}
@media print{
    .onepage {
        page-break-inside: avoid !important; 
        page-break-after: always !important;
    }
}

适用于Snappy和wkhtmltopdf v0.12.2