How to avoid generating PDF twice?

0

after having problems generating the PDF using Firefox to test the code. I generate a pdf using a button inside a form. using the following code.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>TablaPdf</title>
    <script src="jsPDF-1.3.2/dist/jspdf.min.js"></script>
</head>

<body>
    <form>
        <button onclick="generarPdf(event)">Generar</button>
    </form>
    <script type="text/javascript">
        function generarPdf(evt) {
            evt.preventDefault();
            var pdf = new jsPDF();
            pdf.text(20, 20, "Hola Mundo!");
            pdf.save('mipdf.pdf');
            var pdf = new jsPDF();
            pdf.text(40, 20, "Prueba de texto en pdf");
            pdf.save("Primerpdf.pdf");
        }
    </script>
</body>

</html>

which results in that when I click on the button it shows me that the same PDF is downloaded twice

    
asked by Richard Yordy 09.07.2018 в 22:20
source

4 answers

0

The library goes by version 1.4.1, but still loading version 1.3.2 has the same behavior in Chrome and Firefox, although Firefox presents a dialog box for each pdf and Chrome warns of "multiple downloads"

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>TablaPdf</title>
    
</head>

<body>
    <form>
        <button onclick="generarPdf(event)">Generar</button>
    </form>
    <script type="text/javascript">
        function generarPdf(evt) {
            evt.preventDefault();
            var pdf = new jsPDF();
            pdf.text(20, 20, "Hola Mundo!");
            pdf.save('mipdf.pdf');
            var pdf = new jsPDF();
            pdf.text(40, 20, "Prueba de texto en pdf");
            pdf.save("Primerpdf.pdf");
        }
    </script>
</body>

</html>
    
answered by 10.07.2018 / 04:07
source
0

I recommend you use mpdf link , php-based library from HTML files.

    
answered by 09.07.2018 в 22:23
0

Greetings I recommend checking that you include your library perfectly, or check by console if there is an error, this said I show you how the library works

a basic example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello world</title>
</head>
<body>
<input type="button" onclick="miFuncion();" value="Generar PDF">

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
    <script type="text/javascript">
      function miFuncion(){
              var pdf = new jsPDF();
          pdf.setFont('courier')
          pdf.setFontType('bolditalic')
          pdf.text(20, 60, 'Este es mi contenido de pdf')
          pdf.save('nombre_pdf.pdf');

      }

    </script>
</body>
</html>

For this case I use a button when I click on a function (myFunction), which is responsible for generating this pdf.

As I told you, verify that you are loading your library correctly.

I hope it will help and help you ... luck .. !!

Check it here

    
answered by 09.07.2018 в 22:41
0

I use pdfmake and it has gone very well, I recommend it, it is a library in JavaScript that works directly from the client.

here's the documentation

and this is the playground where you can see in code and in real time how it works.

I hope it will help you, if you need help with something, leave me a comment and I'll respond to the act,

Greetings.

    
answered by 09.07.2018 в 23:01