Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
html2canvas + jsPDF multiple pages in IE

I have to print a web page, which is longer than a regular A4 page, from a web app (so basically I need to create a pdf that display ALL my web page in multiple pdf pages). Currently I'm using html2canvas+jsPDF; on chrome they work fine. The problem is that I also need them to work on IE11 and possibily IE9 (I've already solved the "promise" problem by using polyfill).

On IE the function do print the document, but only the first part of the web page (what the user see without scrolling down).

Here is the code:

<script src="${pageContext.request.contextPath}/jsp/shared/silef2/lib/jsPDF/polyfills.es.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/jsp/shared/silef2/lib/jsPDF/jspdf.es.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/jsp/shared/silef2/js/html2canvas.js" type="text/javascript"></script> 


<script>
  function functionPrint()  {

        html2canvas(document.getElementsByTagName("html"),{
            onrendered:function(canvas){

                var imgData = canvas.toDataURL('image/png');
                var imgWidth = 210;
                var pageHeight = 295;
                var imgHeight = canvas.height * imgWidth / canvas.width;
                var heightLeft = imgHeight;
                window.jsPDF = window.jspdf.jsPDF;
                var doc = new jsPDF('p', 'mm');
                var position = 0;

                doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight);
                heightLeft -= pageHeight;

                while (heightLeft >= 0) {
                    position = heightLeft - imgHeight;
                    doc.addPage();
                    doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight);
                    heightLeft -= pageHeight;
                }
                doc.save( 'file.pdf');
            }
        });
    }
</script>

<button onclick="functionPrint();">STAMPA</button>

Unfortunately the console doesn't give me back any error. I suppose there is some kind of incompatibility between jsPDF and IE.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've discovered that IE11 has the property "overflow: hidden" by default so, before printing, you need to override that property manually like this:

<script>
  function functionPrint()  {

        document.getElementById("main").parentNode.style.overflow = 'visible';   <-- THIS

        html2canvas(document.getElementsByTagName("html"),{
            onrendered:function(canvas){

                var imgData = canvas.toDataURL('image/png');
                var imgWidth = 210;
                var pageHeight = 295;
                var imgHeight = canvas.height * imgWidth / canvas.width;
                var heightLeft = imgHeight;
                window.jsPDF = window.jspdf.jsPDF;
                var doc = new jsPDF('p', 'mm');
                var position = 0;

                doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight);
                heightLeft -= pageHeight;

                while (heightLeft >= 0) {
                    position = heightLeft - imgHeight;
                    doc.addPage();
                    doc.addImage(imgData, 'jpeg', 0, position, imgWidth, imgHeight);
                    heightLeft -= pageHeight;
                }
                doc.save( 'file.pdf');
            }
        });
    }
</script>

It only works with getElementById so you need to identify exactly which element compose your page as a whole.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda