1

What is the best way to render this kind of code into pdf. I have looked into different libraries but I do not think any of them is useful in my case. I have detailed looked into TCPDF library but that one is too complicated in my case as my code involves lots of css, html and php conditions. Therefore, I am here to get some hint to know where can I find the something useful in this regard. Following is my code and out of my code:

<div class="box-container" id="week<?php print $w; ?>" >
                <div class="roster">
                    <div style="text-align: right;"  class="dontprint">

                        <div style="display:inline;">
                            <label><strong>Dispatch All</strong> <input type="checkbox" name="dispatch-<?php print $w; ?>" value="<?php print $w; ?>"></label>
                        </div>
                        <div style="display:inline;">
                            <a href="#" class="site-roster" rel="week<?php print $w; ?>">
                                <img src="<?php print base_url('images/icon-screenshot.png'); ?>" />
                            </a>
                        </div>
                    </div>

                    <?php $shiftStr = array();  foreach ($schedule['sites'] as $site): ?>
                        <?php if ($site['weekf'] == 'Week '.$w) : ?>

                        <?php $isDispatched[$w][$site["sid"]] = true; ?>

                            <div class="box printr" id="r<?php print $w.'-'.$site["sid"]; ?>">
                                <h4 class="white" align="center"><?php print $site["title"]; ?> - <?php print $site["address"]; ?></h4>
                                <div class="box-container links" style="border:1px solid #CCC">
                                    <table style="width:100%">
                                        <thead>
                                        <tr>
                                            <td style="width:140px;"></td>
                                            <?php
                                            $dateForRoster = ($w == 1) ? $startDate : strtotime("+7 day", $startDate);
                                            ?>
                                            <?php for ($i = 0; $i < 7; $i++) : ?>
                                                <td><?php print date('d-M-y', strtotime("+".$i." day", $dateForRoster)); ?></td>
                                            <?php endfor; ?>
                                            <td colspan="2"></td>
                                        </tr>
                                        <tr>
                                            <td class="col-left"><?php print $site['job_type'] == 1 ? 'CSO' : 'Guard';?></td>
                                            <td>Monday</td>
                                            <td>Tuesday</td>
                                            <td>Wednesday</td>
                                            <td>Thursday</td>
                                            <td>Friday</td>
                                            <td>Saturday</td>
                                            <td>Sunday</td>
                                            <td>Contact</td>
                                            <td>License</td>
                                        </tr>
                                        <tr>
                                            <td colspan="10"><div style="border-bottom:2px solid #ccc; margin:5px 0"></div></td>
                                        </tr>
                                        </thead>
                                        <?php foreach($schedule['guards'] as $guard): ?>
                                            <?php if ($guard['weekf'] == 'Week '.$w && $site['sid'] == $guard['sid']) : ?>
                                                <tr><?php $tot = 0;?>
                                                    <td style="max-width: 140px; overflow: hidden; white-space: nowrap;" class="col-left"><?php print ($guard["guard_name"] != '') ? $guard["guard_name"] : 'Click on each shift to assign guard'; ?></td>

                                                    <?php for ($s = 0; $s < 7; $s++) : ?>
                                                        <td>
                                                            <?php

                                                            if (isset ($schedule['week'][str_replace(' ', '-', $guard['weekf']).'-'.$site['sid'].'-'.$guard['gid']][$s])) {
                                                                $shifts = $schedule['week'][str_replace(' ', '-', $guard['weekf']).'-'.$site['sid'].'-'.$guard['gid']][$s];
                                                                $noOfShifts = count($shifts);

                                                                if ($noOfShifts == 0) { print '-'; }
                                                                else {
                                                                    foreach($shifts as $shift) {

                                                                        $shiftStr[] = array($shift['shift']['shid'], $shift['shift']['dispatched']);
                                                                        $allShifts[] = array($shift['shift']['shid'], $shift['shift']['dispatched']);
                                                                        //print $tot += dateDifference1($shift['shift']['actual_sign_in'], $shift['shift']['actual_sign_out']);
                                                                        $class = $shift['shift']['job_type'];
                                                                        $difference = dateDifference(date('Y-m-d'), $shift['shift']['advised_sign_in']);

                                                                        if ($shift['shift']['gid'] == "") {
                                                                            $class = "unassigned";
                                                                        } else {
                                                                            if ($shift['shift']['training'] == 1)  $class = "training";
                                                                            if ($difference == 0 &&
                                                                                date("Y-m-d", strtotime($shift['shift']['actual_sign_in'])) == "1970-01-01") {
                                                                                $class = "notime";
                                                                            }

                                                                            if (
                                                                                ($shift['shift']['actual_sign_in'] != "" && $shift['shift']['actual_sign_out'] == "") ||
                                                                                ($shift['shift']['actual_sign_in'] == "" && $shift['shift']['actual_sign_out'] != "")
                                                                                ) 
                                                                            {
                                                                                $class = "onlyone";
                                                                            }

                                                                             if (
                                                                                ($shift['shift']['actual_sign_in'] != "" && ($shift['shift']['advised_sign_in'] != $shift['shift']['actual_sign_in'])) ||
                                                                                ($shift['shift']['actual_sign_out'] != "" && ($shift['shift']['advised_sign_out'] != $shift['shift']['actual_sign_out']))
                                                                            ) {
                                                                                $class = "altertime";
                                                                            }
                                                                        }

                                                                        if ($shift['shift']['dispatched'] == "0") $isDispatched[$w][$site["sid"]] = false;

                                                            ?>

                                                                        <a href="#"
                                                                           class = "shift-detail <?php print $class; ?>"
                                                                           shid = "<?php print $shift['shift']['shid'];?>"
                                                                           date = "<?php print date('Y-m-d', strtotime("+".$s." day", $dateForRoster)); ?>"
                                                                           jid = "<?php print $shift['shift']['jid']; ?>"
                                                                           sid = "<?php print $site['sid']; ?>"
                                                                           gid = "<?php print $guard['gid']; ?>">
                                                                            <?php
                                                                                if (/*$difference > 0 &&*/ $shift['shift']['actual_sign_in'] != "" && $shift['shift']['actual_sign_out'] != "" && date("Y-m-d", strtotime($shift['shift']['actual_sign_in'])) != "1970-01-01")
                                                                                    print date('H:i', strtotime($shift['shift']['actual_sign_in'])).' - '.date('H:i', strtotime($shift['shift']['actual_sign_out'])) ;
                                                                                else
                                                                                    print date('H:i', strtotime($shift['shift']['advised_sign_in'])).' - '.date('H:i', strtotime($shift['shift']['advised_sign_out'])) ;
                                                                            ?></a>

                                                                    <?php
                                                                    }
                                                                }
                                                            } else {
                                                                ?>
                                                                <a href="#"
                                                                   class="shift-detail"
                                                                   shid = "0"
                                                                   date = "<?php print date('Y-m-d', strtotime("+".$s." day", $dateForRoster)); ?>"
                                                                   jid = "0"
                                                                   sid = "<?php print $site['sid']; ?>"
                                                                   gid = "<?php print $guard['gid']; ?>">
                                                                    -
                                                                </a>
                                                            <?php
                                                            }
                                                            ?>
                                                        </td>
                                                    <?php endfor; ?>
                                                    <td><a href="#" class="contact"><?php print $guard['mobile_number']; ?></a></td>
                                                    <td><a href="#" class="license"><?php print ($guard['license_number'] != 0) ? $guard['license_number'] : 'N/A'; ?></a></td>
                                                </tr> <?php //print $tot;?>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </table>

                                    <div style="text-align: right; margin-top:4px; padding-top: 8px; color: #50575A; font-weight:bold; border-top:1px dotted" class="dontprint">

                                        <div style="display:inline; ">
                                            <label>Dispatch <?php print $site["title"]; ?> shifts <input type="checkbox" name="dispatch-<?php print $w.'-'.$site["sid"]; ?>" id="dispatch-<?php print $w.'-'.$site["sid"]; ?>" <?php print ($isDispatched[$w][$site["sid"]]) ? 'checked="checked"' : '' ;?> value='<?php print json_encode($shiftStr);?>'></label>
                                        </div>
                                        <div style="display:inline;">
                                            <a href="#" class="site-roster" rel="r<?php print $w.'-'.$site["sid"]; ?>">
                                                <img src="<?php print base_url('images/icon-screenshot.png'); ?>" />
                                            </a>
                                        </div>
                                    </div>


                                </div>


                                <!-- end of div.box-container -->
                            </div>

                        <?php unset($shiftStr); endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>
            <input type="hidden" name="allshifts-<?php print $w;?>" id="allshifts-<?php print $w;?>" value='<?php print json_encode($allShifts); ?>' /> 
        <?php endfor; ?>
    <?php endif; ?>
</div>

Output

enter image description here

I am looking to directly render this kind of output into pdf. Alternatively, what I am thinking that I should capture the image first then render that image into PDF but that would be double work.

Thus, If you have any idea that could help me in any way please share here.

Thank you.

RK.
  • 973
  • 5
  • 19
  • 45

2 Answers2

3

I have used DOMPDF, as it worked very well. Keep the generated HTML code in a variable and pass it to DOMPDF, like this:

$dompdf = new DOMPDF();
$dompdf->load_html($my_pdf_content);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();

After this, you can either save the file:

file_put_contents('my_pdf.pdf', $dompdf->output());

Or force download into the browser:

$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
exit(0);
machineaddict
  • 3,216
  • 8
  • 37
  • 61
  • I created my PDFs using DOMPDF but i believe there is speed performance issue in this library. It takes 50 to 60 secs to generate 10 pages of PDFs which i believe is not acceptable. DO you know any way that could help me to get rid of this issue ? – RK. Oct 03 '14 at 15:34
  • No. I only use it for printing invoices. At maximum, I have 3 pages. You could also try to cache those pdf files. If there are only a few records to create as pdf or if all of the records are static, you could run a script to create those pdf. The only problem with this approach is the HDD space. – machineaddict Oct 06 '14 at 07:43
  • I came here by searching for "php pdf generation overflow:hidden". I am having problem with exactly thiss CSS code: (copied from the topic starter): "max-width: 140px; overflow: hidden; white-space: nowrap;". I use exactly DOMPDF and what it does is cuts the text but keeps the row width large like the text was not cut! I voted down.. – user109764 Feb 24 '17 at 20:51
  • You voted down because the search engine misguided you and that the solution doesn't fit your problem!? – machineaddict Feb 27 '17 at 12:32
  • Your anger misguided you. Read carefully. I had exactly similar problem as topic starter. Maybe partly but in his example this CSS code is present: style="max-width: 140px; overflow: hidden; white-space: nowrap;" which failed to be generated properly with DOMPDF. I had to apply PHP text cutting instead of a nice CSS solution. – user109764 Mar 10 '17 at 19:27
1

You can also try this one TCPDF it is easy to use PHP to PDF library.. you can also check the examples here

MuntingInsekto
  • 1,543
  • 4
  • 19
  • 36