【问题标题】:Render HTML into PDF file将 HTML 渲染成 PDF 文件
【发布时间】:2014-09-26 04:39:45
【问题描述】:

将这种代码呈现为 pdf 的最佳方法是什么。我研究了不同的库,但我认为它们中的任何一个对我来说都没有用。我已经详细研究了 TCPDF 库,但在我的情况下这个库太复杂了,因为我的代码涉及很多 css、html 和 php 条件。因此,我在这里得到一些提示,以了解在哪里可以找到这方面有用的东西。以下是我的代码和我的代码:

<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>

输出

我希望将这种输出直接呈现为 pdf。或者,我认为我应该先捕获图像,然后将该图像渲染为 PDF,但这将是双重工作。

因此,如果您有任何可以帮助我的想法,请在此处分享。

谢谢。

【问题讨论】:

标签: php html codeigniter pdf


【解决方案1】:

我用过DOMPDF,因为它工作得很好。将生成的 HTML 代码保存在变量中并将其传递给 DOMPDF,如下所示:

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

在此之后,您可以保存文件:

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

或者强制下载到浏览器中:

$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
exit(0);

【讨论】:

  • 我使用 DOMPDF 创建了我的 PDF,但我相信这个库中存在速度性能问题。生成 10 页 PDF 需要 50 到 60 秒,我认为这是不可接受的。你知道有什么方法可以帮助我摆脱这个问题吗?
  • 没有。我只用它来打印发票。最多,我有 3 页。您也可以尝试缓存这些 pdf 文件。如果只有少数记录要创建为 pdf,或者所有记录都是静态的,则可以运行脚本来创建这些 pdf。这种方法的唯一问题是硬盘空间。
  • 我是通过搜索“php pdf generation overflow:hidden”来到这里的。我对这个 CSS 代码有问题:(从主题启动器复制):“最大宽度:140px;溢出:隐藏;空白:nowrap;”。我完全使用 DOMPDF,它的作用是剪切文本,但保持行宽很大,就像文本没有被剪切一样!我投了反对票..
  • 您投了反对票,因为搜索引擎误导了您,并且该解决方案不适合您的问题!?
  • 你的愤怒误导了你。仔细阅读。我有与主题启动器完全相同的问题。也许部分,但在他的例子中,这个 CSS 代码存在: style="max-width: 140px; overflow: hidden; white-space: nowrap;"无法使用 DOMPDF 正确生成。我不得不应用 PHP 文本切割而不是一个不错的 CSS 解决方案。
【解决方案2】:

你也可以试试这个TCPDF很容易使用PHP到PDF库..你也可以查看示例here

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2018-02-16
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多