Skip to content

Commit

Permalink
PDF wordt nu gegenereerd voor alle pagina's in plaats van enkel de zi…
Browse files Browse the repository at this point in the history
…chtbare pagina.
  • Loading branch information
igoethal committed Oct 15, 2024
1 parent cabea42 commit a6df590
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 136 deletions.
2 changes: 1 addition & 1 deletion builddate.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var CONF_builddate="20241014-135814"
var CONF_builddate="20241015-223338"
59 changes: 27 additions & 32 deletions eendraadschema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
Expand Down Expand Up @@ -6191,11 +6191,10 @@ function updateDPI() {
}
function dopdfdownload() {
updateDPI();
printPDF(document.getElementById("printsvgarea").innerHTML, structure.print_table.papersize, //Page format can be "A4" and "A3", nothing else
structure.print_table.pages[structure.print_table.displaypage].stop - structure.print_table.pages[structure.print_table.displaypage].start,
/*structure.print_table.pages[structure.print_table.displaypage].height, */
structure.print_table.stopy - structure.print_table.starty, structure.properties.owner, structure.properties.installer, structure.properties.control, structure.properties.info, structure.properties.dpi, structure.print_table.displaypage + 1, //starts counting at zero so we need to add 1
structure.print_table.pages.length, document.getElementById("dopdfname").value, //filename
//let page = structure.print_table.displaypage+1; //starts counting at zero so we need to add 1
var svg = flattenSVGfromString(structure.toSVG(0, "horizontal").data);
var pages = Array.from({ length: structure.print_table.pages.length }, function (_, i) { return i + 1; });
printPDF(svg, /*document.getElementById("printsvgarea").innerHTML*/ structure.print_table, structure.properties, pages, document.getElementById("dopdfname").value, //filename
document.getElementById("progress_pdf") //HTML element where callback status can be given
);
}
Expand Down Expand Up @@ -6235,12 +6234,12 @@ function renderAddressStacked() {
'</table>';
return outHTML;
}
function getPrintSVGWithoutAddress() {
function getPrintSVGWithoutAddress(page) {
if (page === void 0) { page = structure.print_table.displaypage; }
var outSVG = new SVGelement();
outSVG = structure.toSVG(0, "horizontal");
var scale = 1;
//var height = outSVG.yup + outSVG.ydown;
var page = structure.print_table.displaypage;
var startx = structure.print_table.pages[page].start;
var width = structure.print_table.pages[page].stop - startx;
var starty = structure.print_table.getstarty();
Expand Down Expand Up @@ -6275,15 +6274,29 @@ function printsvg() {
var width = outSVG.xleft + outSVG.xright;
structure.print_table.setHeight(height);
structure.print_table.setMaxWidth(width);
strleft += '<br><button onclick="dopdfdownload()">Genereer PDF</button>';
switch (structure.print_table.getPaperSize()) {
case "A3":
strleft += '&nbsp;<select onchange="HLChangePaperSize()" id="id_papersize"><option value="A4">A4</option><option value="A3" selected="selected">A3</option></select>';
break;
case "A4":
default: strleft += '&nbsp;<select onchange="HLChangePaperSize()" id="id_papersize"><option value="A4" selected="Selected">A4</option><option value="A3">A3</option></select>';
}
if (structure.properties.dpi == 600) {
strleft += '&nbsp;<select id="dpiSelect" onchange="updateDPI()"><option value="300">300dpi (standaard)</option><option value="600" selected>600dpi (beter maar trager)</option></select>';
}
else {
strleft += '&nbsp;<select id="dpiSelect" onchange="updateDPI()"><option value="300" selected>300dpi (standaard)</option><option value="600">600dpi (beter maar trager)</option></select>';
}
strleft += '&nbsp;<input id="dopdfname" size="20" value="eendraadschema_print.pdf">&nbsp;&nbsp;<span id="progress_pdf"></span><br>';
strleft += '<br><table border="0"><tr><td style="vertical-align:top;">';
strleft += structure.print_table.toHTML() + '<br>';
strleft += '</td><td style="vertical-align:top;padding:5px">';
strleft += 'Klik op de groene pijl om het schema over meerdere pagina\'s te printen en kies voor elke pagina de start- en stop-positie in het schema (in pixels). '
+ '<br><br>Onderaan kan je bekijken welk deel van het schema op welke pagina belandt en de pagina exporteren en/of omzetten naar PDF. '
+ "Het exporteren of omzetten naar PDF dient voor elke pagina herhaald te worden.";
+ '<br><br>Onderaan kan je bekijken welk deel van het schema op welke pagina belandt. ';
strleft += '</td></tr></table>';
strleft += '<hr>';
strleft += 'Pagina <select onchange="HLDisplayPage()" id="id_select_page">';
strleft += '<b>Printvoorbeeld: </b>Pagina <select onchange="HLDisplayPage()" id="id_select_page">';
for (var i = 0; i < structure.print_table.pages.length; i++) {
if (i == structure.print_table.displaypage) {
strleft += '<option value=' + (i + 1) + ' selected>' + (i + 1) + '</option>';
Expand All @@ -6293,16 +6306,6 @@ function printsvg() {
}
}
strleft += '</select>&nbsp;&nbsp;';
strleft += 'Layout ';
switch (structure.print_table.getPaperSize()) {
case "A3":
strleft += '<select onchange="HLChangePaperSize()" id="id_papersize"><option value="A4">A4</option><option value="A3" selected="selected">A3</option></select>';
break;
case "A4":
strleft += '<select onchange="HLChangePaperSize()" id="id_papersize"><option value="A4" selected="Selected">A4</option><option value="A3">A3</option></select>';
default:
}
strleft += '&nbsp;&nbsp;';
switch (structure.print_table.getModeVertical()) {
case "kies":
strleft += 'Hoogte <select onchange="HLChangeModeVertical()" id="id_modeVerticalSelect"><option value="alles">Alles (standaard)</option><option value="kies" selected="Selected">Kies (expert)</option></select>';
Expand All @@ -6316,15 +6319,7 @@ function printsvg() {
strleft += 'Hoogte <select onchange="HLChangeModeVertical()" id="id_modeVerticalSelect"><option value="alles">Alles (standaard)</option><option value="kies">Kies (expert)</option></select>';
}
strleft += '<br><br>';
strleft += '<table border="0"><tr><td style="vertical-align:top"><button onclick="dosvgdownload()">Download SVG</button></td><td>&nbsp;</td><td style="vertical-align:top"><input id="dosvgname" size="20" value="eendraadschema_print.svg"></td><td>&nbsp;&nbsp;</td><td>Sla tekening hieronder op als SVG en converteer met een ander programma naar PDF (bvb Inkscape).</td></tr></table><br>';
strleft += '<table border="0"><tr><td style="vertical-align:top"><button onclick="dopdfdownload()">Maak PDF</button></td>';
if (structure.properties.dpi == 600) {
strleft += '<td style="vertical-align:top"><select id="dpiSelect" onchange="updateDPI()"><option value="300">300dpi (standaard)</option><option value="600" selected>600dpi (beter maar trager)</option></select></td>';
}
else {
strleft += '<td style="vertical-align:top"><select id="dpiSelect" onchange="updateDPI()"><option value="300" selected>300dpi (standaard)</option><option value="600">600dpi (beter maar trager)</option></select></td>';
}
strleft += '<td>&nbsp;</td><td style="vertical-align:top"><input id="dopdfname" size="20" value="eendraadschema_print.pdf"></td><td>&nbsp;&nbsp;</td><td id="progress_pdf">Sla tekening dadelijk op als PDF.</td></tr></table><br>';
strleft += '<table border="0"><tr><td style="vertical-align:top"><button onclick="dosvgdownload()">Zichtbare pagina als SVG opslaan</button></td><td>&nbsp;</td><td style="vertical-align:top"><input id="dosvgname" size="20" value="eendraadschema_print.svg"></td><td>&nbsp;&nbsp;</td><td>Sla tekening hieronder op als SVG en converteer met een ander programma naar PDF (bvb Inkscape).</td></tr></table><br>';
strleft += displayButtonPrintToPdf();
strleft += '<div id="printarea"></div>';
document.getElementById("configsection").innerHTML = strleft;
Expand Down
96 changes: 61 additions & 35 deletions jsPDF/print.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", control="", info="", DPI=300, page=1, maxpage=1, filename="eendraadschema_print.pdf", statuscallback) { // Defaults to A4 and 300 DPI but 600 DPI is better
function printPDF(svg, print_table, properties, pages=[1], filename="eendraadschema_print.pdf", statuscallback) { // Defaults to A4 and 300 DPI but 600 DPI is better

if (format=="A3") {
if (print_table.papersize=="A3") {
paperdetails = { // All sizes in millimeters
paperwidth: 420,
paperheight: 297,
Expand Down Expand Up @@ -38,8 +38,8 @@ function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", contro
let max_height_in_mm = paperdetails.paperheight - 2 * paperdetails.paper_margin - paperdetails.owner_box_height - paperdetails.drawnby_box_height - paperdetails.svg_padding;
let max_width_in_mm = paperdetails.paperwidth - 2 * paperdetails.paper_margin;

let max_height_in_pixels = max_height_in_mm/25.4*DPI;
let max_width_in_pixels = max_width_in_mm/25.4*DPI;
let max_height_in_pixels = max_height_in_mm/25.4*properties.dpi;
let max_width_in_pixels = max_width_in_mm/25.4*properties.dpi;

let scale = Math.min(max_height_in_pixels/sizey, max_width_in_pixels/sizex);

Expand Down Expand Up @@ -69,9 +69,6 @@ function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", contro
// Create a temporary element to hold the HTML
const tempElement = document.createElement('div');
tempElement.innerHTML = html;

// Replace <br> tags with \n
//tempElement.innerHTML = tempElement.innerHTML.replace(/<br>/g, "\n");

// Use the textContent property to get the Unicode string
const unicodeString = tempElement.textContent || tempElement.innerText || '';
Expand All @@ -90,28 +87,34 @@ function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", contro
return printlines;
}

// ___ FUNCTION generatePDF ___
//
// Makes the actual PDF

function generatePDF(svg, sizex, sizey) {
function init() {
const { jsPDF } = window.jspdf;
var doc;

if (format=="A3") {
if (print_table.papersize=="A3") {
doc = new jsPDF('landscape', 'mm', 'a3', true);
} else {
doc = new jsPDF('landscape', 'mm', 'a4', true);
}

return(doc);
}

// ___ FUNCTION generatePDF ___
//
// Makes the actual PDF

function addPage(doc, svg, sizex, sizey, callback, iter=0) {
const { jsPDF } = window.jspdf;

svgToPng(svg, sizex, sizey, function(png,scale) {

let canvasx = (paperdetails.paperwidth - 2*paperdetails.paper_margin);
let canvasy = (paperdetails.paperheight - 2*paperdetails.paper_margin - paperdetails.owner_box_height - paperdetails.drawnby_box_height - paperdetails.svg_padding);

if (sizex/sizey > canvasx/canvasy) { //width is leading
let max_height_in_mm = paperdetails.paperheight - 2 * paperdetails.paper_margin - paperdetails.owner_box_height - paperdetails.drawnby_box_height - paperdetails.svg_padding;
//let max_height_in_pixels = max_height_in_mm/25.4*DPI;
//let max_height_in_pixels = max_height_in_mm/25.4*properties.dpi;
let shiftdown = (max_height_in_mm - sizey/sizex*canvasx)/2;
doc.addImage(png, 'PNG', paperdetails.paper_margin, paperdetails.paper_margin+shiftdown, canvasx, sizey/sizex * canvasx, undefined, 'FAST');
} else { //height is leading
Expand Down Expand Up @@ -160,7 +163,7 @@ function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", contro
startx + 2, // Leave 2mm at the left of the drawn by text
paperdetails.paperheight - paperdetails.paper_margin - (paperdetails.drawnby_box_height-textHeight)/2 - textHeight/6);

doc.text('pagina. ' + page + '/' + maxpage,
doc.text('pagina. ' + pages[iter] + '/' + print_table.pages.length,
startx + 3 * paperdetails.owner_box_width + 2, //Leave 2mm at the left
paperdetails.paperheight - paperdetails.paper_margin - paperdetails.drawnby_box_height - paperdetails.owner_box_height - textHeight/6 + textHeight + 1.5 ); //Leave 1.55mm at the top

Expand All @@ -182,42 +185,65 @@ function printPDF(svg, format="A4", sizex, sizey, owner="", installer="", contro

doc.setFont("helvetica", "normal");

doc.text(htmlToPDFlines(doc,control).slice(0,8),
doc.text(htmlToPDFlines(doc,properties.control).slice(0,8),
startx + 2 + 3,
paperdetails.paperheight - paperdetails.paper_margin - paperdetails.drawnby_box_height - paperdetails.owner_box_height - textHeight/6 + textHeight * (1+1.2) + 1.5);

doc.text(htmlToPDFlines(doc,owner).slice(0,8),
doc.text(htmlToPDFlines(doc,properties.owner).slice(0,8),
startx + paperdetails.owner_box_width + 2 + 3,
paperdetails.paperheight - paperdetails.paper_margin - paperdetails.drawnby_box_height - paperdetails.owner_box_height - textHeight/6 + textHeight * (1+1.2) + 1.5);

doc.text(htmlToPDFlines(doc,installer).slice(0,8),
doc.text(htmlToPDFlines(doc,properties.installer).slice(0,8),
startx + (2*paperdetails.owner_box_width) + 2 + 3,
paperdetails.paperheight - paperdetails.paper_margin - paperdetails.drawnby_box_height - paperdetails.owner_box_height - textHeight/6 + textHeight * (1+1.2) + 1.5);

let infoshorter = info.replace('https://www.eendraadschema.goethals-jacobs.be','eendraadschema');
let infoshorter = properties.info.replace('https://www.eendraadschema.goethals-jacobs.be','eendraadschema');
doc.text(htmlToPDFlines(doc,infoshorter).slice(0,8),
startx + (3*paperdetails.owner_box_width) + 2 + 3,
paperdetails.paperheight - paperdetails.paper_margin - paperdetails.drawnby_box_height - paperdetails.owner_box_height - textHeight/6 + textHeight * (1+3*1.2) + 1.5);

doc.save(filename);
statuscallback.innerHTML = 'PDF is klaar. Kijk in uw Downloads folder indien deze niet spontaan wordt geopend.';

//--- functions that could be useful some day ---
//const textWidth = doc.getTextWidth("Page 1");
//doc.addPage();

// Open PDF in a new tab with the correct filename
//const pdfBlob = doc.output('blob');
//doc.output('dataurlnewwindow');
/*const pdfBlob = doc.output('pdfjsnewwindow');
const url = URL.createObjectURL(pdfBlob);
window.open(url, '_blank');*/

//window.open(doc.output('bloburl'), '_blank');
callback(doc, iter+1);
});
}

function cropSVG(svg, page) {

let startx = print_table.pages[page].start;
let width = print_table.pages[page].stop;
let starty = print_table.starty;
let height = print_table.stopy - starty;

let viewbox = '' + startx + ' ' + starty + ' ' + width + ' ' + height;

let outsvg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" transform="scale(1,1)" style="border:1px solid white" ' +
'height="' + (height) + '" width="' + (width) + '" viewBox="' + viewbox + '">' +
svg + '</svg>';

return(outsvg);
}

function nextpage(doc, iter=0) {

if (iter < pages.length ) {
statuscallback.innerHTML = 'Pagina ' + pages[iter] + ' wordt gegenereerd. Even geduld..';
if (iter > 0) doc.addPage();
let sizex = print_table.pages[pages[iter]-1].stop - print_table.pages[pages[iter]-1].start;
let sizey = print_table.stopy - print_table.starty;
addPage(doc, cropSVG(svg, pages[iter]-1), sizex, sizey, nextpage, iter); //add one more page and callback here
} else {
save(doc); //we are done
}
}

function save(doc) {
doc.save(filename);
statuscallback.innerHTML = 'PDF is klaar. Kijk in uw Downloads folder indien deze niet spontaan wordt geopend.';
}

statuscallback.innerHTML = 'PDF wordt gegenereerd. Even geduld..';
generatePDF(svg, sizex, sizey);

doc = init();
nextpage(doc, 0)


}
Loading

0 comments on commit a6df590

Please sign in to comment.