已下载的程序文件为空。

huangapple go评论78阅读模式
英文:

The downloaded files of my program are empty

问题

程序应该接收一个数字,然后生成与给定数字相同数量的表格。

程序能够正确生成表格,但当我尝试下载它们时,它们是空的。

以下是代码部分:

window.jsPDF = window.jspdf.jsPDF;

function criarTabelas() {
  var container = document.getElementById("container");
  container.innerHTML = ""; // 清空容器的先前内容

  var repeticoes = parseInt(document.getElementById("repeticoes").value, 10) || 0; // 用户输入的值

  for (var i = 0; i < repeticoes; i++) {
    var div = document.createElement("div");
    div.className = "tabelaBingo";
    var tabela = document.createElement("table");
    tabela.innerHTML = "<caption>Tabela de Bingo de Genes " + (i + 1) + "</caption>";

    for (var j = 0; j < 4; j++) {
      var resultados;
      if (j === 0) {
        resultados = gerarValorAleatorio(combinacoes1);
      } else if (j === 1) {
        resultados = gerarValorAleatorio(combinacoes2);
      } else if (j === 2) {
        resultados = gerarValorAleatorio(combinacoes3);
      } else if (j === 3) {
        resultados = gerarValorAleatorio(combinacoes4);
      }

      var tr = document.createElement("tr");
      tabela.appendChild(tr);
      for (var k = 0; k < 4; k++) {
        var valor = resultados[k];

        var td = document.createElement("td");
        td.textContent = valor;
        tr.appendChild(td);
      }
    }

    div.appendChild(tabela);
    container.appendChild(div);
  }
}

var combinacoes = ["UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG", "CUU", "CUC", "CUA", "CUG", "CCU", "CCC", "CCA", "CCG", "CAU", "CAC", "CAA", "CAG", "CGU", "CGC", "CGA", "CGG", "AUU", "AUC", "AUA", "AUG", "ACU", "ACC", "ACA", "ACG", "AAU", "AAC", "AAA", "AAG", "AGU", "AGC", "AGA", "AGG", "GUU", "GUC", "GUA", "GUG", "GCU", "GCC", "GCA", "GCG", "GAU", "GAC", "GAA", "GAG", "GGU", "GGC", "GGA", "GGG"];
var combinacoes1 = ["UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG"];
var combinacoes2 = ["CUU", "CUC", "CUA", "CUG", "CCU", "CCC", "CCA", "CCG", "CAU", "CAC", "CAA", "CAG", "CGU", "CGC", "CGA", "CGG"];
var combinacoes3 = ["AUU", "AUC", "AUA", "AUG", "ACU", "ACC", "ACA", "ACG", "AAU", "AAC", "AAA", "AAG", "AGU", "AGC", "AGA", "AGG"];
var combinacoes4 = ["GUU", "GUC", "GUA", "GUG", "GCU", "GCC", "GCA", "GCG", "GAU", "GAC", "GAA", "GAG", "GGU", "GGC", "GGA", "GGG"];

function gerarValorAleatorio(valores) {
  var temp = valores.slice();
  var valoresAleatorios = [];

  for (var i = 0; i < 4; i++) {
    var indice = Math.floor(Math.random() * temp.length);
    valoresAleatorios.push(temp[indice]);
    temp.splice(indice, 1);
  }
  return valoresAleatorios;
}

function CriaPDF() {
  var tabelaBingo = document.getElementsByClassName("tabelaBingo");

  for (var i = 0; i < tabelaBingo.length; i++) {
    var tabela = tabelaBingo[i];

    var doc = new jsPDF(); // 创建新的PDF文档

    // 将表格转换为HTML字符串
    var htmlString = new XMLSerializer().serializeToString(tabela);

    // 设置表格的样式选项
    var style = "<style>table { width: 100%; font-size: 12px; } th, td { padding: 5px; }</style>";

    // 拼接样式和表格内容
    var content = style + htmlString;

    // 将转换后的内容添加到PDF文档中
    doc.html(content);

    // 以适当的名称保存PDF
    doc.save("bingo" + (i + 1) + ".pdf");
  }
}

这段代码生成文件没有问题,但当我下载并打开它们时,它们是空的。希望这可以帮助你解决问题。

英文:

Simple put exactly what the title says.

The program should receive a number and then generate as many tables as the given number.

The program is able to generate the tables correctly, but when I try to download them, they are empty.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.jsPDF = window.jspdf.jsPDF;
function criarTabelas() {
var container = document.getElementById(&quot;container&quot;);
container.innerHTML = &quot;&quot;; // Limpar o conte&#250;do anterior do container
var repeticoes = parseInt(document.getElementById(&quot;repeticoes&quot;).value, 10) || 0; // Valor inserido pelo usu&#225;rio
for (var i = 0; i &lt; repeticoes; i++) {
var div = document.createElement(&quot;div&quot;);
div.className = &quot;tabelaBingo&quot;;
var tabela = document.createElement(&quot;table&quot;);
tabela.innerHTML = &quot;&lt;caption&gt;Tabela de Bingo de Genes &quot; + (i + 1) + &quot;&lt;/caption&gt;&quot;;
for (var j = 0; j &lt; 4; j++) {
var resultados;
if (j === 0) {
resultados = gerarValorAleatorio(combinacoes1);
} else if (j === 1) {
resultados = gerarValorAleatorio(combinacoes2);
} else if (j === 2) {
resultados = gerarValorAleatorio(combinacoes3);
} else if (j === 3) {
resultados = gerarValorAleatorio(combinacoes4);
}
var tr = document.createElement(&quot;tr&quot;);
tabela.appendChild(tr);
for (var k = 0; k &lt; 4; k++) {
var valor = resultados[k];
var td = document.createElement(&quot;td&quot;);
td.textContent = valor;
tr.appendChild(td);
}
}
div.appendChild(tabela);
container.appendChild(div);
}
}
var combinacoes = [&quot;UUU&quot;, &quot;UUC&quot;, &quot;UUA&quot;, &quot;UUG&quot;, &quot;UCU&quot;, &quot;UCC&quot;, &quot;UCA&quot;, &quot;UCG&quot;, &quot;UAU&quot;, &quot;UAC&quot;, &quot;UAA&quot;, &quot;UAG&quot;, &quot;UGU&quot;, &quot;UGC&quot;, &quot;UGA&quot;, &quot;UGG&quot;, &quot;CUU&quot;, &quot;CUC&quot;, &quot;CUA&quot;, &quot;CUG&quot;, &quot;CCU&quot;, &quot;CCC&quot;, &quot;CCA&quot;, &quot;CCG&quot;, &quot;CAU&quot;, &quot;CAC&quot;, &quot;CAA&quot;, &quot;CAG&quot;, &quot;CGU&quot;, &quot;CGC&quot;, &quot;CGA&quot;, &quot;CGG&quot;, &quot;AUU&quot;, &quot;AUC&quot;, &quot;AUA&quot;, &quot;AUG&quot;, &quot;ACU&quot;, &quot;ACC&quot;, &quot;ACA&quot;, &quot;ACG&quot;, &quot;AAU&quot;, &quot;AAC&quot;, &quot;AAA&quot;, &quot;AAG&quot;, &quot;AGU&quot;, &quot;AGC&quot;, &quot;AGA&quot;, &quot;AGG&quot;, &quot;GUU&quot;, &quot;GUC&quot;, &quot;GUA&quot;, &quot;GUG&quot;, &quot;GCU&quot;, &quot;GCC&quot;, &quot;GCA&quot;, &quot;GCG&quot;, &quot;GAU&quot;, &quot;GAC&quot;, &quot;GAA&quot;, &quot;GAG&quot;, &quot;GGU&quot;, &quot;GGC&quot;, &quot;GGA&quot;, &quot;GGG&quot;];
var combinacoes1 = [&quot;UUU&quot;, &quot;UUC&quot;, &quot;UUA&quot;, &quot;UUG&quot;, &quot;UCU&quot;, &quot;UCC&quot;, &quot;UCA&quot;, &quot;UCG&quot;, &quot;UAU&quot;, &quot;UAC&quot;, &quot;UAA&quot;, &quot;UAG&quot;, &quot;UGU&quot;, &quot;UGC&quot;, &quot;UGA&quot;, &quot;UGG&quot;];
var combinacoes2 = [&quot;CUU&quot;, &quot;CUC&quot;, &quot;CUA&quot;, &quot;CUG&quot;, &quot;CCU&quot;, &quot;CCC&quot;, &quot;CCA&quot;, &quot;CCG&quot;, &quot;CAU&quot;, &quot;CAC&quot;, &quot;CAA&quot;, &quot;CAG&quot;, &quot;CGU&quot;, &quot;CGC&quot;, &quot;CGA&quot;, &quot;CGG&quot;];
var combinacoes3 = [&quot;AUU&quot;, &quot;AUC&quot;, &quot;AUA&quot;, &quot;AUG&quot;, &quot;ACU&quot;, &quot;ACC&quot;, &quot;ACA&quot;, &quot;ACG&quot;, &quot;AAU&quot;, &quot;AAC&quot;, &quot;AAA&quot;, &quot;AAG&quot;, &quot;AGU&quot;, &quot;AGC&quot;, &quot;AGA&quot;, &quot;AGG&quot;];
var combinacoes4 = [&quot;GUU&quot;, &quot;GUC&quot;, &quot;GUA&quot;, &quot;GUG&quot;, &quot;GCU&quot;, &quot;GCC&quot;, &quot;GCA&quot;, &quot;GCG&quot;, &quot;GAU&quot;, &quot;GAC&quot;, &quot;GAA&quot;, &quot;GAG&quot;, &quot;GGU&quot;, &quot;GGC&quot;, &quot;GGA&quot;, &quot;GGG&quot;];
function gerarValorAleatorio(valores) {
var temp = valores.slice();
var valoresAleatorios = [];
for (var i = 0; i &lt; 4; i++) {
var indice = Math.floor(Math.random() * temp.length);
valoresAleatorios.push(temp[indice]);
temp.splice(indice, 1);
}
return valoresAleatorios;
}
function CriaPDF() {
var tabelaBingo = document.getElementsByClassName(&quot;tabelaBingo&quot;);
for (var i = 0; i &lt; tabelaBingo.length; i++) {
var tabela = tabelaBingo[i];
var doc = new jsPDF(); // Criar um novo documento PDF
// Converter a tabela em HTML para uma string de conte&#250;do
var htmlString = new XMLSerializer().serializeToString(tabela);
// Definir as op&#231;&#245;es de estilo para a tabela
var style = &quot;&lt;style&gt;table { width: 100%; font-size: 12px; } th, td { padding: 5px; }&lt;/style&gt;&quot;;
// Concatenar o estilo e o conte&#250;do da tabela
var content = style + htmlString;
// Adicionar o conte&#250;do convertido ao documento PDF
doc.html(content);
// Salvar o PDF com o nome adequado
doc.save(&quot;bingo&quot; + (i + 1) + &quot;.pdf&quot;);
}
}

<!-- language: lang-css -->

table {
width: 700px;
height: 500px;
font: 17px Calibri;
margin-bottom: 20px;
}
table,
th,
td {
border: solid 1px #DDD;
border-collapse: collapse;
padding: 2px 3px;
text-align: center;
}

<!-- language: lang-html -->

&lt;h1&gt;Tabelas de Bingo&lt;/h1&gt;
&lt;label for=&quot;repeticoes&quot;&gt;N&#250;mero de Repeti&#231;&#245;es:&lt;/label&gt;
&lt;input type=&quot;number&quot; id=&quot;repeticoes&quot; value=&quot;3&quot;&gt;
&lt;button onclick=&quot;criarTabelas()&quot;&gt;Gerar Tabelas&lt;/button&gt;
&lt;button onclick=&quot;CriaPDF()&quot;&gt;Baixar PDF&lt;/button&gt;
&lt;div id=&quot;container&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.1/purify.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://html2canvas.hertzen.com/dist/html2canvas.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

This generate the files just right but the moment I download and open then they are empty.
By the way the rumblings in the code are portuguese

I have tried using the librarys suggested and followed the instructions but still there is this single problem of the tables being empty when I download then

答案1

得分: 2

你的代码存在问题,问题在于你试图使用jsPDF将HTML表格转换为PDF的方法不正确。你使用的doc.html()方法在jsPDF中不是有效的方法。相反,你可以使用html2canvas库来捕获HTML内容作为图像,然后将该图像添加到PDF中。

我编辑了代码并将其放入了一个txt文件中(链接如下),现在应该可以正常工作。

<!DOCTYPE html>
<html>
<head>
    <title>Tabelas de Bingo</title>

    <style>
        table {
            width: 700px;
            height: 500px;
            font: 17px Calibri;
            margin-bottom: 20px;
        }

        table, th, td {
            border: solid 1px #DDD;
            border-collapse: collapse;
            padding: 2px 3px;
            text-align: center;
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.1/purify.min.js"></script>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>

<body>

<script>
    window.jsPDF = window.jspdf.jsPDF;

    function criarTabelas() {
        var container = document.getElementById("container");
        container.innerHTML = ""; // Limpar o conteúdo anterior do container

        var repeticoes = parseInt(document.getElementById("repeticoes").value, 10) || 0; // Valor inserido pelo usuário

        for (var i = 0; i < repeticoes; i++) {
            var div = document.createElement("div");
            div.className = "tabelaBingo";
            var tabela = document.createElement("table");
            tabela.innerHTML = "<caption>Tabela de Bingo de Genes " + (i + 1) + "</caption>";

            for (var j = 0; j < 4; j++) {
                var resultados;
                if (j === 0) {
                    resultados = gerarValorAleatorio(combinacoes1);
                } else if (j === 1) {
                    resultados = gerarValorAleatorio(combinacoes2);
                } else if (j === 2) {
                    resultados = gerarValorAleatorio(combinacoes3);
                } else if (j === 3) {
                    resultados = gerarValorAleatorio(combinacoes4);
                }

                var tr = document.createElement("tr");
                tabela.appendChild(tr);
                for (var k = 0; k < 4; k++) {
                    var valor = resultados[k];

                    var td = document.createElement("td");
                    td.textContent = valor;
                    tr.appendChild(td);
                }
            }

            div.appendChild(tabela);
            container.appendChild(div);
        }
    }

    var combinacoes = ["UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG"];
    var combinacoes1 = ["UUU", "UUC", "UUA", "UUG", "UCU", "UCC", "UCA", "UCG", "UAU", "UAC", "UAA", "UAG", "UGU", "UGC", "UGA", "UGG"];
    var combinacoes2 = ["CUU", "CUC", "CUA", "CUG", "CCU", "CCC", "CCA", "CCG", "CAU", "CAC", "CAA", "CAG", "CGU", "CGC", "CGA", "CGG"];
    var combinacoes3 = ["AUU", "AUC", "AUA", "AUG", "ACU", "ACC", "ACA", "ACG", "AAU", "AAC", "AAA", "AAG", "AGU", "AGC", "AGA", "AGG"];
    var combinacoes4 = ["GUU", "GUC", "GUA", "GUG", "GCU", "GCC", "GCA", "GCG", "GAU", "GAC", "GAA", "GAG", "GGU", "GGC", "GGA", "GGG"];

    function gerarValorAleatorio(valores) {
        var temp = valores.slice();
        var valoresAleatorios = [];

        for (var i = 0; i < 4; i++) {
            var indice = Math.floor(Math.random() * temp.length);
            valoresAleatorios.push(temp[indice]);
            temp.splice(indice, 1);
        }
        return valoresAleatorios;
    }

    function CriaPDF() {
        var tabelaBingo = document.getElementsByClassName("tabelaBingo");

        var promises = []; // Array to store promises for each table capture

        for (var i = 0; i < tabelaBingo.length; i++) {
            var tabela = tabelaBingo[i];

            var promise = html2canvas(tabela).then(function (canvas) {
                var imgData = canvas.toDataURL("image/png");

                var doc = new jsPDF(); // Criar um novo documento PDF

                // Define the dimensions of the PDF page according to the captured image
                var imgWidth = 210; // A4 page width in mm
                var imgHeight = canvas.height * imgWidth / canvas.width;

                // Add the captured image to the PDF
                doc.addImage(imgData, 'PNG', 0, 0, imgWidth, imgHeight);

                // Save the PDF with the appropriate name
                doc.save("bingo" + (i + 1) + ".pdf");
            });

            promises.push(promise);
        }

        // Wait for all promises to resolve
        Promise.all(promises).then(function () {
            console.log("PDFs generated successfully.");
        });
    }
</script>

<h1>Tabelas de Bingo</h1>
<label for="repeticoes">Número de Repetições:</label>
<input type="number" id="repeticoes" value="3">
<button onclick="criarTabelas()">Gerar Tabelas</button>
<button onclick="CriaPDF()">Baixar PDF</button>
<div id="container"></div>

</body>
</html>

链接: 点击这里 下载代码文件。

英文:

The issue with your code lies in the way you're trying to convert the HTML table into a PDF using jsPDF. The doc.html() method you're using is not a valid method in jsPDF. Instead, you can use the html2canvas library to capture the HTML content as an image and then add that image to the PDF.
I edited the code and put it in a txt file(link below) and it should work fine now.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Tabelas de Bingo&lt;/title&gt;
&lt;style&gt;
table {
width: 700px;
height: 500px;
font: 17px Calibri;
margin-bottom: 20px;
}
table, th, td {
border: solid 1px #DDD;
border-collapse: collapse;
padding: 2px 3px;
text-align: center;
}
&lt;/style&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.1/purify.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://html2canvas.hertzen.com/dist/html2canvas.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script&gt;
window.jsPDF = window.jspdf.jsPDF;
function criarTabelas() {
var container = document.getElementById(&quot;container&quot;);
container.innerHTML = &quot;&quot;; // Limpar o conte&#250;do anterior do container
var repeticoes = parseInt(document.getElementById(&quot;repeticoes&quot;).value, 10) || 0; // Valor inserido pelo usu&#225;rio
for (var i = 0; i &lt; repeticoes; i++) {
var div = document.createElement(&quot;div&quot;);
div.className = &quot;tabelaBingo&quot;;
var tabela = document.createElement(&quot;table&quot;);
tabela.innerHTML = &quot;&lt;caption&gt;Tabela de Bingo de Genes &quot; + (i + 1) + &quot;&lt;/caption&gt;&quot;;
for (var j = 0; j &lt; 4; j++) {
var resultados;
if (j === 0) {
resultados = gerarValorAleatorio(combinacoes1);
} else if (j === 1) {
resultados = gerarValorAleatorio(combinacoes2);
} else if (j === 2) {
resultados = gerarValorAleatorio(combinacoes3);
} else if (j === 3) {
resultados = gerarValorAleatorio(combinacoes4);
}
var tr = document.createElement(&quot;tr&quot;);
tabela.appendChild(tr);
for (var k = 0; k &lt; 4; k++) {
var valor = resultados[k];
var td = document.createElement(&quot;td&quot;);
td.textContent = valor;
tr.appendChild(td);
}
}
div.appendChild(tabela);
container.appendChild(div);
}
}
var combinacoes = [&quot;UUU&quot;, &quot;UUC&quot;, &quot;UUA&quot;, &quot;UUG&quot;, &quot;UCU&quot;, &quot;UCC&quot;, &quot;UCA&quot;, &quot;UCG&quot;, &quot;UAU&quot;, &quot;UAC&quot;, &quot;UAA&quot;, &quot;UAG&quot;, &quot;UGU&quot;, &quot;UGC&quot;, &quot;UGA&quot;, &quot;UGG&quot;, &quot;CUU&quot;, &quot;CUC&quot;, &quot;CUA&quot;, &quot;CUG&quot;, &quot;CCU&quot;, &quot;CCC&quot;, &quot;CCA&quot;, &quot;CCG&quot;, &quot;CAU&quot;, &quot;CAC&quot;, &quot;CAA&quot;, &quot;CAG&quot;, &quot;CGU&quot;, &quot;CGC&quot;, &quot;CGA&quot;, &quot;CGG&quot;, &quot;AUU&quot;, &quot;AUC&quot;, &quot;AUA&quot;, &quot;AUG&quot;, &quot;ACU&quot;, &quot;ACC&quot;, &quot;ACA&quot;, &quot;ACG&quot;, &quot;AAU&quot;, &quot;AAC&quot;, &quot;AAA&quot;, &quot;AAG&quot;, &quot;AGU&quot;, &quot;AGC&quot;, &quot;AGA&quot;, &quot;AGG&quot;, &quot;GUU&quot;, &quot;GUC&quot;, &quot;GUA&quot;, &quot;GUG&quot;, &quot;GCU&quot;, &quot;GCC&quot;, &quot;GCA&quot;, &quot;GCG&quot;, &quot;GAU&quot;, &quot;GAC&quot;, &quot;GAA&quot;, &quot;GAG&quot;, &quot;GGU&quot;, &quot;GGC&quot;, &quot;GGA&quot;, &quot;GGG&quot;];
var combinacoes1 = [&quot;UUU&quot;, &quot;UUC&quot;, &quot;UUA&quot;, &quot;UUG&quot;, &quot;UCU&quot;, &quot;UCC&quot;, &quot;UCA&quot;, &quot;UCG&quot;, &quot;UAU&quot;, &quot;UAC&quot;, &quot;UAA&quot;, &quot;UAG&quot;, &quot;UGU&quot;, &quot;UGC&quot;, &quot;UGA&quot;, &quot;UGG&quot;];
var combinacoes2 = [&quot;CUU&quot;, &quot;CUC&quot;, &quot;CUA&quot;, &quot;CUG&quot;, &quot;CCU&quot;, &quot;CCC&quot;, &quot;CCA&quot;, &quot;CCG&quot;, &quot;CAU&quot;, &quot;CAC&quot;, &quot;CAA&quot;, &quot;CAG&quot;, &quot;CGU&quot;, &quot;CGC&quot;, &quot;CGA&quot;, &quot;CGG&quot;];
var combinacoes3 = [&quot;AUU&quot;, &quot;AUC&quot;, &quot;AUA&quot;, &quot;AUG&quot;, &quot;ACU&quot;, &quot;ACC&quot;, &quot;ACA&quot;, &quot;ACG&quot;, &quot;AAU&quot;, &quot;AAC&quot;, &quot;AAA&quot;, &quot;AAG&quot;, &quot;AGU&quot;, &quot;AGC&quot;, &quot;AGA&quot;, &quot;AGG&quot;];
var combinacoes4 = [&quot;GUU&quot;, &quot;GUC&quot;, &quot;GUA&quot;, &quot;GUG&quot;, &quot;GCU&quot;, &quot;GCC&quot;, &quot;GCA&quot;, &quot;GCG&quot;, &quot;GAU&quot;, &quot;GAC&quot;, &quot;GAA&quot;, &quot;GAG&quot;, &quot;GGU&quot;, &quot;GGC&quot;, &quot;GGA&quot;, &quot;GGG&quot;];
function gerarValorAleatorio(valores) {
var temp = valores.slice();
var valoresAleatorios = [];
for (var i = 0; i &lt; 4; i++) {
var indice = Math.floor(Math.random() * temp.length);
valoresAleatorios.push(temp[indice]);
temp.splice(indice, 1);
}
return valoresAleatorios;
}
function CriaPDF() {
var tabelaBingo = document.getElementsByClassName(&quot;tabelaBingo&quot;);
var promises = []; // Array to store promises for each table capture
for (var i = 0; i &lt; tabelaBingo.length; i++) {
var tabela = tabelaBingo[i];
var promise = html2canvas(tabela).then(function (canvas) {
var imgData = canvas.toDataURL(&quot;image/png&quot;);
var doc = new jsPDF(); // Criar um novo documento PDF
// Define the dimensions of the PDF page according to the captured image
var imgWidth = 210; // A4 page width in mm
var imgHeight = canvas.height * imgWidth / canvas.width;
// Add the captured image to the PDF
doc.addImage(imgData, &#39;PNG&#39;, 0, 0, imgWidth, imgHeight);
// Save the PDF with the appropriate name
doc.save(&quot;bingo&quot; + (i + 1) + &quot;.pdf&quot;);
});
promises.push(promise);
}
// Wait for all promises to resolve
Promise.all(promises).then(function () {
console.log(&quot;PDFs generated successfully.&quot;);
});
}
&lt;/script&gt;
&lt;h1&gt;Tabelas de Bingo&lt;/h1&gt;
&lt;label for=&quot;repeticoes&quot;&gt;N&#250;mero de Repeti&#231;&#245;es:&lt;/label&gt;
&lt;input type=&quot;number&quot; id=&quot;repeticoes&quot; value=&quot;3&quot;&gt;
&lt;button onclick=&quot;criarTabelas()&quot;&gt;Gerar Tabelas&lt;/button&gt;
&lt;button onclick=&quot;CriaPDF()&quot;&gt;Baixar PDF&lt;/button&gt;
&lt;div id=&quot;container&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年6月15日 12:34:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定