Print the contents of a DIV in PDF

0

I have an application in which I generate PDF417 codes, I tell them I have a button that takes the information from a TEXTAREA and with a dll or a library generates a PDF417 code, what I'm looking for is that DIV that generates a button to print it in PDF OR WORD or some option to send them to a pdf set in a grid, to print the labels.

Or failing to convert it into an image and export it to pdf or word

<div id="barcode"></div>

This is my div generated, thanks, regards!

    
asked by EduardoVelazquez 10.07.2017 в 22:31
source

2 answers

1

You can use CSS to choose which elements to print when you run the event on the button.

    <style media="print">
        #sidebar, .header, .site-footer, .invoice-btn, #titulo {
            display: none;
        }
        #contenido_DIV {
            display:block;
        }
    </style>

<a onclick="javascript:javascript:window.print();" >Print</a>

The only drawback I see is that you have to define the selectors well so that only what you want to see is visible in the print window.

You can also do it using javascript, which I think is best for you. You call the function with the name of the DIV that you want to print, then you open the print window in a new tab with the content of the div and when closing the dialog the tab will close automatically.

<div id="ID_DIV">

</div>
<a onclick="javascript:window.imprimirDIV('ID_DIV');">Print </a>
<script>
    function imprimirDIV(contenido) {
        var ficha = document.getElementById(contenido);
        var ventanaImpresion = window.open(' ', 'popUp');
        ventanaImpresion.document.write(ficha.innerHTML);
        ventanaImpresion.document.close();
        ventanaImpresion.print();
        ventanaImpresion.close();
    }
</script>
    
answered by 11.07.2017 / 00:12
source
1

I recommend you the library " html2Canvas "

I would do something like this:

<script src="~/Scripts/html2canvas.min.js"></script>
<script>
    html2canvas($("#barcode"), {
                onrendered: function (canvas) {
                    var win=window.open();
                    win.document.write("<br><img src='"+canvas.toDataURL()+"'/>");
                    win.print();
                }
            });
</script>
    
answered by 10.07.2017 в 23:11