comparison client/src/components/Pdftool.vue @ 2714:4dd38840d74b

client:pdf-gen:imporve some template elements * add property to define the border color of the elements * fix rounding of boxes to handle case that box is not rounded * check text for small version of date
author Fadi Abbud <fadi.abbud@intevation.de>
date Mon, 18 Mar 2019 17:52:04 +0100
parents 4d5f419a2318
children 4a5c0e7cb75b
comparison
equal deleted inserted replaced
2713:b79f5c5404c2 2714:4dd38840d74b
305 let defaultFontSize = 11, 305 let defaultFontSize = 11,
306 defaultRounding = 2, 306 defaultRounding = 2,
307 defaultTextColor = "black", 307 defaultTextColor = "black",
308 defaultBgColor = "white", 308 defaultBgColor = "white",
309 defaultPadding = 3, 309 defaultPadding = 3,
310 defaultOffset = { x: 0, y: 0 }; 310 defaultOffset = { x: 0, y: 0 },
311 defaultBorderColor = "white";
311 this.templateData.elements.forEach(e => { 312 this.templateData.elements.forEach(e => {
312 switch (e.type) { 313 switch (e.type) {
313 case "text": { 314 case "text": {
314 this.addText( 315 this.addText(
315 e.position, 316 e.position,
327 e.offset || defaultOffset, 328 e.offset || defaultOffset,
328 e.width, 329 e.width,
329 e.height, 330 e.height,
330 // handling the case when the rectangle not rounded (rounding = 0) 331 // handling the case when the rectangle not rounded (rounding = 0)
331 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding, 332 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
332 e.color || defaultBgColor 333 e.color || defaultBgColor,
334 e.brcolor || defaultBorderColor
333 ); 335 );
334 break; 336 break;
335 } 337 }
336 case "textbox": { 338 case "textbox": {
337 this.addTextBox( 339 this.addTextBox(
342 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding, 344 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
343 e.padding || defaultPadding, 345 e.padding || defaultPadding,
344 e.fontSize || defaultFontSize, 346 e.fontSize || defaultFontSize,
345 e.color || defaultTextColor, 347 e.color || defaultTextColor,
346 e.background || defaultBgColor, 348 e.background || defaultBgColor,
347 e.text 349 e.text,
350 e.brcolor || defaultBorderColor
348 ); 351 );
349 break; 352 break;
350 } 353 }
351 case "image": { 354 case "image": {
352 this.addImage( 355 this.addImage(
361 } 364 }
362 case "bottleneck": { 365 case "bottleneck": {
363 this.addBottleneckInfo( 366 this.addBottleneckInfo(
364 e.position, 367 e.position,
365 e.offset || defaultOffset, 368 e.offset || defaultOffset,
366 e.rounding || defaultRounding, 369 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
367 e.color || defaultTextColor 370 e.color || defaultTextColor,
371 e.brcolor || defaultBorderColor
368 ); 372 );
369 break; 373 break;
370 } 374 }
371 case "legend": { 375 case "legend": {
372 this.addLegend( 376 this.addLegend(
373 e.position, 377 e.position,
374 e.offset || defaultOffset, 378 e.offset || defaultOffset,
375 e.rounding || defaultRounding 379 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
380 e.brcolor || defaultBorderColor
376 ); 381 );
377 break; 382 break;
378 } 383 }
379 case "scalebar": { 384 case "scalebar": {
380 this.addScaleBar( 385 this.addScaleBar(
381 scaleNominator, 386 scaleNominator,
382 e.position, 387 e.position,
383 e.offset || defaultOffset, 388 e.offset || defaultOffset,
384 e.rounding || defaultRounding 389 e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
390 e.brcolor || defaultBorderColor
385 ); 391 );
386 break; 392 break;
387 } 393 }
388 case "northarrow": { 394 case "northarrow": {
389 this.addNorthArrow( 395 this.addNorthArrow(
423 ); 429 );
424 this.openLayersMap.setSize(this.mapSize); 430 this.openLayersMap.setSize(this.mapSize);
425 this.openLayersMap.getView().fit(this.mapExtent, { size: this.mapSize }); 431 this.openLayersMap.getView().fit(this.mapExtent, { size: this.mapSize });
426 this.readyToGenerate = true; 432 this.readyToGenerate = true;
427 }, 433 },
428 addRoundedBox(x, y, w, h, color, rounding) { 434 addRoundedBox(x, y, w, h, color, rounding, brcolor) {
429 // draws a rounded background box at (x,y) width x height 435 // draws a rounded background box at (x,y) width x height
430 // using jsPDF units 436 // using jsPDF units
431 this.pdf.doc.setDrawColor(color); 437 this.pdf.doc.setDrawColor(brcolor);
432 this.pdf.doc.setFillColor(color); 438 this.pdf.doc.setFillColor(color);
433 this.pdf.doc.roundedRect(x, y, w, h, rounding, rounding, "FD"); 439 this.pdf.doc.roundedRect(x, y, w, h, rounding, rounding, "FD");
434 }, 440 },
435 // add some text at specific coordinates and determine how many wrolds in single line 441 // add some text at specific coordinates and determine how many wrolds in single line
436 addText(position, offset, width, fontSize, color, text) { 442 addText(position, offset, width, fontSize, color, text) {
455 y = this.pdf.height - offset.y - this.getTextHeight(textLines.length); 461 y = this.pdf.height - offset.y - this.getTextHeight(textLines.length);
456 } 462 }
457 463
458 this.pdf.doc.text(textLines, x, y, { baseline: "hanging" }); 464 this.pdf.doc.text(textLines, x, y, { baseline: "hanging" });
459 }, 465 },
460 addBox(position, offset, width, height, rounding, color) { 466 addBox(position, offset, width, height, rounding, color, brcolor) {
461 // x/y defaults to offset for topleft corner (normal x/y coordinates) 467 // x/y defaults to offset for topleft corner (normal x/y coordinates)
462 let x = offset.x; 468 let x = offset.x;
463 let y = offset.y; 469 let y = offset.y;
464 470
465 // if position is on the right, x needs to be calculate with pdf width and 471 // if position is on the right, x needs to be calculate with pdf width and
469 } 475 }
470 if (["bottomright", "bottomleft"].indexOf(position) !== -1) { 476 if (["bottomright", "bottomleft"].indexOf(position) !== -1) {
471 y = this.pdf.height - offset.y - height; 477 y = this.pdf.height - offset.y - height;
472 } 478 }
473 479
474 this.addRoundedBox(x, y, width, height, color, rounding); 480 this.addRoundedBox(x, y, width, height, color, rounding, brcolor);
475 }, 481 },
476 // add some text at specific coordinates with a background box 482 // add some text at specific coordinates with a background box
477 addTextBox( 483 addTextBox(
478 position, 484 position,
479 offset, 485 offset,
482 rounding, 488 rounding,
483 padding, 489 padding,
484 fontSize, 490 fontSize,
485 color, 491 color,
486 background, 492 background,
487 text 493 text,
494 brcolor
488 ) { 495 ) {
489 this.pdf.doc.setFontSize(fontSize); 496 this.pdf.doc.setFontSize(fontSize);
490 text = this.replacePlaceholders(text); 497 text = this.replacePlaceholders(text);
491 498
492 if (!width) { 499 if (!width) {
496 if (!height) { 503 if (!height) {
497 let textLines = this.pdf.doc.splitTextToSize(text, textWidth); 504 let textLines = this.pdf.doc.splitTextToSize(text, textWidth);
498 height = this.getTextHeight(textLines.length) + 2 * padding; 505 height = this.getTextHeight(textLines.length) + 2 * padding;
499 } 506 }
500 507
501 this.addBox(position, offset, width, height, rounding, background); 508 this.addBox(
509 position,
510 offset,
511 width,
512 height,
513 rounding,
514 background,
515 brcolor
516 );
502 this.addText( 517 this.addText(
503 position, 518 position,
504 { x: offset.x + padding, y: offset.y + padding }, 519 { x: offset.x + padding, y: offset.y + padding },
505 textWidth, 520 textWidth,
506 fontSize, 521 fontSize,
533 } 548 }
534 } 549 }
535 550
536 this.pdf.doc.addImage(image, x, y, width, height); 551 this.pdf.doc.addImage(image, x, y, width, height);
537 }, 552 },
538 addScaleBar(scaleNominator, position, offset, rounding) { 553 addScaleBar(scaleNominator, position, offset, rounding, brcolor) {
539 // scaleNominator is the x in 1:x of the map scale 554 // scaleNominator is the x in 1:x of the map scale
540 555
541 // hardcode maximal width for now 556 // hardcode maximal width for now
542 let maxWidth = 80; // in mm 557 let maxWidth = 80; // in mm
543 558
611 // to give the outer white box 4mm padding 626 // to give the outer white box 4mm padding
612 let scaleBarX = x + 4; 627 let scaleBarX = x + 4;
613 let scaleBarY = y + 5; // 5 because above the scalebar will be the numbers 628 let scaleBarY = y + 5; // 5 because above the scalebar will be the numbers
614 629
615 // draw outer white box 630 // draw outer white box
616 this.addRoundedBox(x, y, fullSize + 8, 10, "white", rounding); 631 this.addRoundedBox(x, y, fullSize + 8, 10, "white", rounding, brcolor);
617 632
618 // draw first part of scalebar 633 // draw first part of scalebar
619 this.pdf.doc.setDrawColor(0, 0, 0); 634 this.pdf.doc.setDrawColor(0, 0, 0);
620 this.pdf.doc.setFillColor(0, 0, 0); 635 this.pdf.doc.setFillColor(0, 0, 0);
621 this.pdf.doc.rect(scaleBarX, scaleBarY, size, 1, "FD"); 636 this.pdf.doc.rect(scaleBarX, scaleBarY, size, 1, "FD");
705 this.pdf.doc.setFontSize(size * 3); 720 this.pdf.doc.setFontSize(size * 3);
706 this.pdf.doc.setTextColor(0, 0, 0); 721 this.pdf.doc.setTextColor(0, 0, 0);
707 this.pdf.doc.setFontStyle("normal"); 722 this.pdf.doc.setFontStyle("normal");
708 this.pdf.doc.text(size < 3 ? x1 - 0.5 : x1 - 1.3, y3 + 1, "N"); 723 this.pdf.doc.text(size < 3 ? x1 - 0.5 : x1 - 1.3, y3 + 1, "N");
709 }, 724 },
710 addLegend(position, offset, rounding) { 725 addLegend(position, offset, rounding, brcolor) {
711 if ( 726 if (
712 this.selectedBottleneck && 727 this.selectedBottleneck &&
713 this.selectedSurvey && 728 this.selectedSurvey &&
714 this.getLayerByName(LAYERS.BOTTLENECKISOLINE).isVisible 729 this.getLayerByName(LAYERS.BOTTLENECKISOLINE).isVisible
715 ) { 730 ) {
734 } 749 }
735 if (["bottomright", "bottomleft"].indexOf(position) !== -1) { 750 if (["bottomright", "bottomleft"].indexOf(position) !== -1) {
736 y = this.pdf.height - offset.y - height; 751 y = this.pdf.height - offset.y - height;
737 } 752 }
738 753
739 this.addRoundedBox(x, y, width, height, "white", rounding); 754 this.addRoundedBox(x, y, width, height, "white", rounding, brcolor);
740 this.pdf.doc.addImage( 755 this.pdf.doc.addImage(
741 legendImage, 756 legendImage,
742 x + padding, 757 x + padding,
743 y + padding, 758 y + padding,
744 width - 2 * padding, 759 width - 2 * padding,
745 height - 2 * padding 760 height - 2 * padding
746 ); 761 );
747 } 762 }
748 }, 763 },
749 addBottleneckInfo(position, offset, rounding, color) { 764 addBottleneckInfo(position, offset, rounding, color, brcolor) {
750 if ( 765 if (
751 this.selectedBottleneck && 766 this.selectedBottleneck &&
752 this.selectedSurvey && 767 this.selectedSurvey &&
753 this.getLayerByName(LAYERS.BOTTLENECKISOLINE).isVisible 768 this.getLayerByName(LAYERS.BOTTLENECKISOLINE).isVisible
754 ) { 769 ) {
809 if (["bottomright", "bottomleft"].indexOf(position) !== -1) { 824 if (["bottomright", "bottomleft"].indexOf(position) !== -1) {
810 y = this.pdf.height - offset.y - height; 825 y = this.pdf.height - offset.y - height;
811 } 826 }
812 827
813 // white background box 828 // white background box
814 this.addRoundedBox(x, y, width, height, "white", rounding); 829 this.addRoundedBox(x, y, width, height, "white", rounding, brcolor);
815 830
816 // bottleneck 831 // bottleneck
817 this.pdf.doc.setFontStyle("italic"); 832 this.pdf.doc.setFontStyle("italic");
818 this.pdf.doc.text(x + padding, y + padding, str1_1, textOptions); 833 this.pdf.doc.text(x + padding, y + padding, str1_1, textOptions);
819 this.pdf.doc.setFontStyle("bold"); 834 this.pdf.doc.setFontStyle("bold");
854 } 869 }
855 }, 870 },
856 replacePlaceholders(text) { 871 replacePlaceholders(text) {
857 if (text.includes("{date}")) { 872 if (text.includes("{date}")) {
858 text = text.replace("{date}", new Date().toLocaleString(locale2)); 873 text = text.replace("{date}", new Date().toLocaleString(locale2));
874 }
875 //get only day,month and year from the Date object
876 if (text.includes("{date-minor}")) {
877 var date = new Date();
878 var dt =
879 (date.getDate() < 10 ? "0" : "") +
880 date.getDate() +
881 "." +
882 (date.getMonth() + 1 < 10 ? "0" : "") +
883 (date.getMonth() + 1) +
884 "." +
885 date.getFullYear();
886 text = text.replace("{date-minor}", dt.toLocaleString(locale2));
859 } 887 }
860 if (text.includes("{user}")) { 888 if (text.includes("{user}")) {
861 text = text.replace("{user}", this.user); 889 text = text.replace("{user}", this.user);
862 } 890 }
863 return text; 891 return text;