【问题标题】:QR CODE not printing二维码不打印
【发布时间】:2016-12-02 18:54:52
【问题描述】:

我生成了一个带有 url 作为数据内容的 QR CODE,但是当我尝试打印生成 qr 的页面时,qr 似乎还没有出现其他内容显示。这是我的sn-p

    <?php

require_once("qrcode.php");

//---------------------------------------------------------

$qr = new QRCode();
// ƒGƒ‰[’ù³ƒŒƒxƒ‹‚ðÝ’è
// QR_ERROR_CORRECT_LEVEL_L : 7%
// QR_ERROR_CORRECT_LEVEL_M : 15%
// QR_ERROR_CORRECT_LEVEL_Q : 25%
// QR_ERROR_CORRECT_LEVEL_H : 30%
$qr->setErrorCorrectLevel(QR_ERROR_CORRECT_LEVEL_L);

$qr->setTypeNumber(4);

$qr->addData("http:/".$_SERVER['REQUEST_URI']."");

$qr->make();

//---------------------------------------------------------


?>

下面是页面上的其他内容

<div class="invoice-box" id="invoice-box">
    <table cellpadding="0" cellspacing="0">
        <tr class="top">
            <td colspan="2">
                <table>
                    <tr>
                        <td class="title">
                            <img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
                        </td>



                        <td>
                                Invoice #: <?php print $invoice_details->sub_code ?><br>
                                Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
                                Due: February 1, 2015
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr class="information">
                <td colspan="2">
                    <table>
                        <tr>
                            <td>
                               <br>
                               <br>

                            </td>

                            <td>
                                <?php print $invoice_details->org ?><br>
                                <?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
                                <?php print $invoice_details->email ?>

                            </td>
                            <td>
                            <?php $qr->printHTML(); ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr class="heading">
                <td>
                    Payment Method
                </td>

                <td>

                </td>
            </tr>

            <tr class="details">
                <td>
                    <?php print $invoice_details->method ?>
                </td>

                <td>

                </td>
            </tr>

            <tr class="heading">
                <td>
                    Item
                </td>

                <td>
                    Price(UGX)
                </td>
            </tr>

            <tr class="item">
                <td>
                   <?php print ucfirst($invoice_details->event) ?> - Summit
                </td>

                <td>
                   <?php print number_format($invoice_details->amount) ?>
                </td>
            </tr>


            <tr class="total">
                <td></td>

                <td>
                   Total:   UGX <?php print number_format($invoice_details->amount) ?>
                </td>
            </tr>
        </table>
    </div>
    <input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />

还有我的打印脚本

<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($(elem).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'invoice-box', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        mywindow.close();

        return true;
    }

</script>

【问题讨论】:

  • $qr-&gt;printHTML(); 返回什么?
  • 返回二维码
  • 据我所知,二维码是图像,而不是 HTML 代码,这就是为什么我想知道它返回的原因。
  • 好吧,我在旅途中生成它,这是由 php 和 qr-code 类促进的,如果这里允许的话,我可以给你一个指向我的项目的链接
  • 我认为只要它与问题相关并且其中没有私人信息是允许的。

标签: php jquery qr-code


【解决方案1】:

假设我找到了正确的库,它有一个image creator。您需要做的是使用imagegif(或imagepng)调用周围的输出缓冲区将图像的二进制数据捕获为字符串,然后对字符串进行base64编码,并将其作为数据URI的一部分直接回显到img 标签的 src 属性。

$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();

在您的 HTML 中

<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">

如果您担心对数据 URL 的支持,请忽略输出缓冲,将唯一的随机文件名作为第二个参数添加到 imagegif 调用以保存它,然后在 src 中回显该文件名。

【讨论】:

  • 如果 OP 验证他们使用的库确实是支持创建图像的库,这将有所帮助。如果是这样,这比使用 HTML 表格可以完成的任何事情都要干净。
  • 我使用了该库,但是页面上的内容如何被二维码的输出隐藏,尝试检查 html,它显示二维码显示在表格中,我试图追踪表格的来源,但都是徒劳的
  • @EgesaDonatius 它显示在表格中,因为您将其显示在表格中。如果图片太大,调整$qr-&gt;createImage(4, 2);行中的数值参数。
【解决方案2】:

您似乎正在尝试打印默认从打印中删除的黑色背景颜色。不幸的是,没有办法在 Safari 和 Chrome 以外的浏览器中以编程方式强制它返回。 您可以通过执行以下操作强制将其恢复为 Safari 和 Chrome:

<div class="invoice-box" id="invoice-box">
    <style> 
        .invoice-box table td: {
            -webkit-print-color-adjust: exact !important;
        }
    </style>
    <table cellpadding="0" cellspacing="0">
        <tr class="top">
            <td colspan="2">
                <table>
                    <tr>
                        <td class="title">
                            <img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
                        </td>



                        <td>
                                Invoice #: <?php print $invoice_details->sub_code ?><br>
                                Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
                                Due: February 1, 2015
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr class="information">
                <td colspan="2">
                    <table>
                        <tr>
                            <td>
                               <br>
                               <br>

                            </td>

                            <td>
                                <?php print $invoice_details->org ?><br>
                                <?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
                                <?php print $invoice_details->email ?>

                            </td>
                            <td>
                            <?php $qr->printHTML(); ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr class="heading">
                <td>
                    Payment Method
                </td>

                <td>

                </td>
            </tr>

            <tr class="details">
                <td>
                    <?php print $invoice_details->method ?>
                </td>

                <td>

                </td>
            </tr>

            <tr class="heading">
                <td>
                    Item
                </td>

                <td>
                    Price(UGX)
                </td>
            </tr>

            <tr class="item">
                <td>
                   <?php print ucfirst($invoice_details->event) ?> - Summit
                </td>

                <td>
                   <?php print number_format($invoice_details->amount) ?>
                </td>
            </tr>


            <tr class="total">
                <td></td>

                <td>
                   Total:   UGX <?php print number_format($invoice_details->amount) ?>
                </td>
            </tr>
        </table>
    </div>
    <input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />

不幸的是,对于像 IE 和 Firefox 这样的浏览器,这需要用户交互。有关更多答案,请参阅此问题:Printing background-color in Firefox and IE

【讨论】:

  • 不能依赖用户更改这些设置,也不需要。
【解决方案3】:

最后,我有办法解决这个问题,这是我的解决方案

    <?php


require_once("qrcode.php");


$qr = QRCode::getMinimumQRCode("http:/".$_SERVER['REQUEST_URI']."", QR_ERROR_CORRECT_LEVEL_L);


$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();

?>

然后用这个将二维码输出为图像;

<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">

感谢@walf 提供的解决方案,希望对其他人有所帮助。

【讨论】:

  • 这不是一个新的答案,这样的编辑应该添加到您的问题中,并注明为编辑。如果我(或任何人)的回答解决了您的问题,请将其标记为已接受。此外,您的网址不完整;试试$qr = QRCode::getMinimumQRCode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], QR_ERROR_CORRECT_LEVEL_L);
猜你喜欢
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 2022-06-10
  • 2014-06-27
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多