Add margin to H-tags in PDF

Margins to H tags and some motion HTML-PDF refinement
This commit is contained in:
Sean Engelhardt 2019-02-19 14:08:47 +01:00
parent f09a39a3c3
commit ee3fb7d7ef
2 changed files with 66 additions and 19 deletions

View File

@ -46,6 +46,11 @@ export class HtmlToPdfService {
*/ */
private P_MARGIN_BOTTOM = 4.0; private P_MARGIN_BOTTOM = 4.0;
/**
* Space above H
*/
private H_MARGIN_TOP = 10.0;
/** /**
* Conversion of HTML tags into pdfmake directives * Conversion of HTML tags into pdfmake directives
*/ */
@ -84,14 +89,44 @@ export class HtmlToPdfService {
*/ */
public constructor() {} public constructor() {}
/**
* Determine the ideal top margin for a given node
*
* @param nodeName the node to parse
* @returns the margin tip as number
*/
private getMarginTop(nodeName: string): number {
switch (nodeName) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6': {
return this.H_MARGIN_TOP;
}
default: {
return 0;
}
}
}
/** /**
* Determine the ideal margin for a given node * Determine the ideal margin for a given node
* *
* @param nodeName the parsing node * @param nodeName the node to parse
* @returns the margin bottom as number * @returns the margin bottom as number
*/ */
private getMarginBottom(nodeName: string): number { private getMarginBottom(nodeName: string): number {
switch (nodeName) { switch (nodeName) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6': {
return this.P_MARGIN_BOTTOM;
}
case 'li': { case 'li': {
return this.LI_MARGIN_BOTTOM; return this.LI_MARGIN_BOTTOM;
} }
@ -126,6 +161,7 @@ export class HtmlToPdfService {
const parsedElement = this.parseElement(child); const parsedElement = this.parseElement(child);
docDef.push(parsedElement); docDef.push(parsedElement);
} }
return docDef; return docDef;
} }
@ -198,20 +234,28 @@ export class HtmlToPdfService {
} }
newParagraph.margin = [0, 0, 0, 0]; newParagraph.margin = [0, 0, 0, 0];
// determine the "normal" top and button margins
newParagraph.margin[1] = this.getMarginTop(nodeName);
newParagraph.margin[3] = this.getMarginBottom(nodeName);
if (this.lineNumberingMode === LineNumberingMode.Outside) { if (this.lineNumberingMode === LineNumberingMode.Outside) {
// that is usually the case for inserted change which should appear // that is usually the case for inserted change which should appear
// under a set of line numbers with correct alignment // under a set of line numbers with correct alignment
if (classes.includes('insert')) { if (classes.includes('insert')) {
newParagraph.margin[0] = 20; newParagraph.margin[0] = 20;
newParagraph.margin[1] = this.P_MARGIN_BOTTOM;
newParagraph.margin[3] = this.P_MARGIN_BOTTOM; newParagraph.margin[3] = this.P_MARGIN_BOTTOM;
} }
} }
// stop enumeration if the list was inserted
if (classes.includes('os-split-before')) { if (classes.includes('os-split-before')) {
newParagraph.listType = 'none'; newParagraph.listType = 'none';
} else if (this.isInsideAList(element)) { }
newParagraph.margin[3] = this.getMarginBottom(nodeName);
// if the list ends (usually due to a new insert cr) prevent margins
if (classes.includes('os-split-after')) {
newParagraph.margin[3] = 0;
} }
newParagraph.lineHeight = this.LINE_HEIGHT; newParagraph.lineHeight = this.LINE_HEIGHT;
@ -266,10 +310,14 @@ export class HtmlToPdfService {
break; break;
} }
case 'br': { case 'br': {
if (this.lineNumberingMode === LineNumberingMode.None && classes.includes('os-line-break')) {
break;
} else {
newParagraph = this.create('text'); newParagraph = this.create('text');
// yep thats all // yep thats all
newParagraph.text = '\n'; newParagraph.text = '\n';
newParagraph.lineHeight = this.LINE_HEIGHT; newParagraph.lineHeight = this.LINE_HEIGHT;
}
break; break;
} }
case 'ul': case 'ul':
@ -302,15 +350,11 @@ export class HtmlToPdfService {
margin: [0, 0, 0, 0] margin: [0, 0, 0, 0]
}; };
// For correction factor for "change reco" elements in lists, cause // This has the effect that changed complex lists will look good with line numbers,
// they open a new OL-List and have additional distance // but simple lists will be too close. The information in the HTML is highly redundant and
if ( // there is currently no clear way to determine what to do with the lists.
element.classList.contains('os-split-before') && if (classes.includes('os-split-after')) {
element.classList.contains('os-split-after') listCol.margin[3] = -this.LI_MARGIN_BOTTOM;
) {
listCol.margin = [0, -this.LI_MARGIN_BOTTOM, 0, -this.LI_MARGIN_BOTTOM];
} else if (!element.classList.contains('os-split-before')) {
listCol.margin = [0, 5, 0, 0];
} }
for (const line of lines) { for (const line of lines) {

View File

@ -1205,9 +1205,12 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit {
* Click handler for the pdf button * Click handler for the pdf button
*/ */
public onDownloadPdf(): void { public onDownloadPdf(): void {
const exportCr = this.motion.isStatuteAmendment() ? ChangeRecoMode.Diff : this.crMode;
// TODO: apparently statue amendments never have line numbers and are always in crMode // TODO: apparently statue amendments never have line numbers and are always in crMode
this.pdfExport.exportSingleMotion(this.motion, this.lnMode, exportCr); if (this.motion.isStatuteAmendment()) {
this.pdfExport.exportSingleMotion(this.motion, LineNumberingMode.None, ChangeRecoMode.Diff);
} else {
this.pdfExport.exportSingleMotion(this.motion, this.lnMode, this.crMode);
}
} }
/** /**