1

I generating a QR CODE with url as data content, but when i try to print a page where the qr is generated, the qr does not seem to appear yet other content displays. Here is my snippet

    <?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();

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


?>

and below is the other content on the page

<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')" />

and my printing script

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

3 Answers3

2

Assuming I found the correct library, it has an image creator. What you need to do is use an output buffer around the imagegif (or imagepng) call to capture the image's binary data into a string, then base64 encode the string, and echo that as part of a data URI directly in the src attribute of an img tag.

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

In your HTML

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

If you're concerned about support for data URLs, leave out the output buffering, add a unique, random filename as the second parameter to the imagegif call to save it, then echo that filename in the src.

Walf
  • 8,535
  • 2
  • 44
  • 59
  • It would help if OP verified that the library they are using is indeed this one with support for creating images. If it is this is much cleaner than anything that can be done using HTML tables. – apokryfos Aug 02 '16 at 08:07
  • i used the library, but some how the content on the page is hidden by the output of the qr code, tried to inspect the html, it shows that the qr-code is displayed in a table, i tried to trace where the table comes from but all in vain – Egesa Donatius Aug 03 '16 at 09:48
  • @EgesaDonatius It was displayed in a table because you were displaying it in a table. If the image was too large, adjust the numeric parameters in the `$qr->createImage(4, 2);` line. – Walf Aug 04 '16 at 00:33
0

It seems that you're trying to print a black background color which is removed by default from printing. Unfortunately there's no way to force it back programatically in browsers other than Safari and Chrome. You can force this back for Safari and Chrome by doing:

<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')" />

For browsers like IE and Firefox unfortunately this requires user interaction. See this question for more answers: Printing background-color in Firefox and IE

Community
  • 1
  • 1
apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

Finally, i got a way around this, here is my solution

    <?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();

?>

Then output the qrcode as image with this;

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

Thanks to @walf for his solution somehow, hope it helps someone else.

  • This is not a new answer, edits like this should be added to your question and noted as an edit. If my (or anyone's) answer solved your problem, mark it as accepted. Also, your URL is not complete; try `$qr = QRCode::getMinimumQRCode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], QR_ERROR_CORRECT_LEVEL_L);` – Walf Aug 04 '16 at 00:37