Last active
July 5, 2018 21:46
-
-
Save lucis/2111c320febcaa265e3d43dc383a568b to your computer and use it in GitHub Desktop.
Converte relatórios do SisDM para o War (quase)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!window.jsPDF) { | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.src = "https://unpkg.com/jspdf@latest/dist/jspdf.min.js"; | |
document.head.appendChild(script); | |
} | |
function arrayBufferToBase64(buffer) { | |
var binary = ""; | |
var bytes = [].slice.call(new Uint8Array(buffer)); | |
bytes.forEach(b => (binary += String.fromCharCode(b))); | |
return window.btoa(binary); | |
} | |
if (!window.getImage) { | |
getImage = i => | |
$(".area_infos_100c")[i] && | |
$(".area_infos_100c") | |
[i].children[0].children[2].children[0].onclick.toString() | |
.replace(/.*\n.*open\('..\//, ""); | |
getRealImage = i => getImage(i) && getImage(i).replace(/'\)\n.*.*/, ""); | |
function getAllReal(title) { | |
const docu = new jsPDF(); | |
lastY = 10; | |
const size = 70; | |
const { "0": _q, ...rest } = $(".area_infos_100c"); | |
const promises = Object.keys(rest) | |
.filter(key => key.length === 1) | |
.filter(key => getRealImage(key).includes(".jpg")) | |
.map(i => | |
fetch(`../${getRealImage(i)}`) | |
.then(res => res.arrayBuffer()) | |
.then(buf => `data:image/jpeg;base64,${arrayBufferToBase64(buf)}`) | |
.then(src => { | |
docu.addImage(src, "JPEG", 10, lastY, 100, size); | |
lastY += size + 10; | |
if (lastY + size + 10 > 297) { | |
docu.addPage(); | |
lastY = 10; | |
} | |
}) | |
); | |
return Promise.all(promises).then(() => docu.save(`${title}.pdf`)); | |
} | |
content = [ | |
, | |
$("#data_hora")[0].value, | |
$("#resumo")[0].value, | |
$("#resultados")[0].value | |
].join("%%"); | |
dataParaEua = data => { | |
return [ | |
data.substring(6, 10), | |
data.substring(3, 5), | |
data.substring(0, 2) | |
].join("-"); | |
}; | |
getCodigoParaWar = (resumo, resultados, data) => { | |
return `$('#id_resumo').val(\`${resumo}\`);$('#id_resultado').val(\`${resultados}\`);$('#id_data_realizacao').val(\`${dataParaEua(data)}\`);$('#id_planejamento').val('Essa atividade foi planejada antes do início da gestão, inclusive já acordado com o palestrante.');$('#id_abrangencia').val('Capítulo, Visitantes e Filhas de Jó');$('#id_conclusao').val('Pessoalmente, promovo muito temas como liberdade religiosa, e foi muito bom ver o envolvimente dos irmãos com o tema, trazendo conhecimento para todos do Capítulo.');`; | |
}; | |
showDados = () => { | |
codigo = window.prompt( | |
"Detalhes do relatório foram capturados. Copie o código abaixo", | |
getCodigoParaWar( | |
$("#resumo")[0].value, | |
$("#resultados")[0].value, | |
$("#data_hora")[0].value | |
) | |
); | |
}; | |
} | |
main = () => { | |
if ($("#data_hora").length) { | |
const nome = window.prompt( | |
"Digite o nome do arquivo desejado. Após isso, você receberá o código para copiar os outros dados do relatório. Após copiar, clique em OK e o PDF com as imagens será baixado automaticamente", | |
$("#categorias option:checked").text() || "Relatorio_SISDM" | |
); | |
if (!nome) { | |
return; | |
} | |
if (!window.jsPDF) { | |
window.setTimeout(() => { | |
getAllReal(nome).then(showDados); | |
}, 1000); | |
} else { | |
getAllReal(nome).then(showDados); | |
} | |
} | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment