英文:
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("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", "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(); // Criar um novo documento PDF
// Converter a tabela em HTML para uma string de conteúdo
var htmlString = new XMLSerializer().serializeToString(tabela);
// Definir as opções de estilo para a tabela
var style = "<style>table { width: 100%; font-size: 12px; } th, td { padding: 5px; }</style>";
// Concatenar o estilo e o conteúdo da tabela
var content = style + htmlString;
// Adicionar o conteúdo convertido ao documento PDF
doc.html(content);
// Salvar o PDF com o nome adequado
doc.save("bingo" + (i + 1) + ".pdf");
}
}
<!-- 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 -->
<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>
<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>
<!-- 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.
<!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", "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");
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论