tablas print para page imprimir generar formato exportar ejemplo con javascript html pdf printing

javascript - print - jspdf



¿Cómo enviar un archivo pdf directamente a la impresora usando JavaScript? (5)

¿Cómo enviar un archivo PDF directamente a la impresora usando JavaScript?

Encontré dos respuestas en un foro:

<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden> <a onClick="document.getElementById(''Pdf1'').printWithDialog()" style="cursor:hand;">Print file</a>

y

<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290"> <PARAM NAME=''SRC'' VALUE="file.pdf"> </OBJECT> <a onClick="document.Pdf2.printWithDialog()">Print file</a>

Pero mi problema es que simplemente funciona en IE y no funciona en Firefox o Chrome.

¿Hay alguna solución para esto?


Creo que esta biblioteca de JavaScript podría ayudarte:

Se llama Print.js

Primero Incluir

<script src="print.js"></script> <link rel="stylesheet" type="text/css" href="print.css">

Su uso básico es llamar a printJS() y simplemente pasar un url de documento PDF: printJS(''docs/PrintJS.pdf'')

Lo que hice fue algo así, esto también mostrará "Cargando ..." si el documento PDF es demasiado grande.

<button type="button" onclick="printJS({printable:''docs/xx_large_printjs.pdf'', type:''pdf'', showModal:true})"> Print PDF with Message </button>

Sin embargo, tenga en cuenta que:

Firefox actualmente no permite imprimir documentos PDF utilizando iframes. Hay un bug abierto en el sitio web de Mozilla sobre esto. Al usar Firefox, Print.js abrirá el archivo PDF en una nueva pestaña.


Hay dos pasos que debes tomar.

En primer lugar, debe colocar el PDF en un iframe.

<iframe id="pdf" name="pdf" src="document.pdf"></iframe>

Para imprimir el iframe, puede ver las respuestas aquí:

Javascript Imprimir solo contenido iframe

Si desea imprimir el iframe automáticamente después de que se haya cargado el PDF, puede agregar un controlador de carga al <iframe> :

<iframe onload="isLoaded()" id="pdf" name="pdf" src="document.pdf"></iframe>

el cargador puede verse así:

function isLoaded() { var pdfFrame = window.frames["pdf"]; pdfFrame.focus(); pdfFrame.print(); }

Esto mostrará el cuadro de diálogo de impresión del navegador y luego solo imprimirá el documento PDF. (Yo personalmente uso el controlador de carga para habilitar un botón "imprimir" para que el usuario decida imprimir el documento, o no).

Estoy usando este código más o menos al pie de la letra en Safari y Chrome, pero todavía no lo intento en IE o Firefox.


Pruebe esto: tenga un botón / enlace que abra una página web (en una nueva ventana) con solo el archivo pdf incrustado en ella, e imprima la página web.

En el encabezado de la página principal:

<script type="text/javascript"> function printpdf() { myWindow=window.open("pdfwebpage.html"); myWindow.close; //optional, to close the new window as soon as it opens //this ensures user doesn''t have to close the pop-up manually } </script>

Y en el cuerpo de la página principal:

<a href="printpdf()">Click to Print the PDF</a>

Dentro de pdfwebpage.html:

<html> <head> </head> <body onload="window.print()"> <embed src="pdfhere.pdf"/> </body> </html>


una función para albergar el disparador de impresión ...

function printTrigger(elementId) { var getMyFrame = document.getElementById(elementId); getMyFrame.focus(); getMyFrame.contentWindow.print(); }

un botón para dar acceso al usuario ...

(an onClick on an a or button or input or whatever you wish) <input type="button" value="Print" onclick="printTrigger(''iFramePdf'');" /> an iframe pointing to your PDF... <iframe id="iFramePdf" src="myPdfUrl.pdf" style="dispaly:none;"></iframe>

Más: http://www.fpdf.org/en/script/script36.php


<?php $browser_ver = get_browser(null,true); //echo $browser_ver[''browser'']; if($browser_ver[''browser''] == ''IE'') { ?> <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><strong class="highlight">pdf</strong> <strong class="highlight">print</strong> test</title> <style> html { height:100%; } </style> <<strong class="highlight">script</strong>> function printIt(id){ var <strong class="highlight">pdf</strong> = document.getElementById("samplePDF"); <strong class="highlight">pdf</strong>.click(); <strong class="highlight">pdf</strong>.setActive(); <strong class="highlight">pdf</strong>.focus(); <strong class="highlight">pdf</strong>.print(); } </script> </head> <body style="margin:0; height:100%;"> <embed id="samplePDF" type="application/pdf" src="/pdfs/2010/dash_fdm350.pdf" width="100%" height="100%" /> <button onClick="printIt(''samplePDF'')"><strong class="highlight">Print</strong></button> </body> </html> <?php } else { ?> <HTML> <<strong class="highlight">script</strong> Language="javascript"> function printfile(id) { window.frames[id].focus(); window.frames[id].print(); } </script> <BODY marginheight="0" marginwidth="0"> <iframe src="/pdfs/2010/dash_fdm350.pdf" id="objAdobePrint" name="objAdobePrint" height="95%" width="100%" frameborder=0></iframe><br> <input type="button" value="<strong class="highlight">Print</strong>" onclick="javascript<b></b>: printfile(''objAdobePrint'');"> </BODY> </HTML> <?php } ?>