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

186
Visualizações
html2canvas and jsPDF, promise error, createObjectURL is not a Function

I'm working a piece of functionality that takes content from a Bootstrap modal, saves it to a PDF and downloads the file to my local PC.

Here's an excerpt from my HTML and listItem.js file

function downloadPDF() {
  // some stuff

  html2canvas($("#printItems")[0], {
    allowTaint: true
  }).then(function(canvas) {
    var imgData = canvas.toDataURL('image/jpeg, 1.0');
    var pdf = jsPDF({
      unit: 'mm',
      format: 'a4',
      orientation: 'portrait'
    });

    pdf.addImage(imgData, 'jpeg', 15, 2);
    pdf.save('test.pdf');
  });
}
<div class="modal-body" id="printItems">
  <div class="container" id="selectedItems"></div>  
</div>

<script type="text/javascript" src="/library/html2canvas.min.js"></script>
<script type="text/javascript" src="/library/jspdf.min.js"></script>

The problem I'm having is when I hit pdf.save('test.pdf') the function fails and I get the following error:

Uncaught(in promise) TypeError: l(...).createObjectURL is not a function

I'm not sure what I'm doing wrong here. Also, I'm working with version 1.5.3 of jsPDF and 1.1.4 of html2canvas

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

0

This was a very strange issue, but I figured out what was wrong and it really had nothing to do with the jspdf or html2canvas versions I was using. For some reason jspdf did not like me referencing a URL in two of my other scripts. Once I removed the actual URLs(https) and just had the physical path it worked fine. I was going down the wrong road looking into versioning and figuring out different ways to write a promise. LOL and SMH

The code I posted above works now.

about 4 years ago · Juan Pablo Isaza Relatório

0

I tried this and seems working without any problem. check the versions that I used from CDN in the below snippet.

I think @Rory McCrossan is right about trying new versions.

<html>
<header>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
    <script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js"></script>
</header>

<body>

    <div class="modal-body" id="printItems">
        <div class="container" id="selectedItems"> Some Text...</div>
    </div>

    <script>
        function downloadPDF() {
            html2canvas($("#printItems")[0], {
                allowTaint: true
            }).then(function (canvas) {
                var imgData = canvas.toDataURL('image/jpeg, 1.0');
                var pdf = new jspdf.jsPDF({
                    unit: 'mm',
                    format: 'a4',
                    orientation: 'portrait'
                });

                pdf.addImage(imgData, 'jpeg', 15, 2);
                pdf.save('test.pdf');
            });
        }
        downloadPDF()
    </script>
</body>

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