Merge pull request #4300 from tsiegleauq/pdf-line-number-fixes

Add line-numbering fix
This commit is contained in:
Emanuel Schütze 2019-02-11 17:42:44 +01:00 committed by GitHub
commit 12ca09e97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 20 deletions

View File

@ -75,6 +75,23 @@ export class HtmlToPdfService {
*/
public constructor() {}
/**
* Determine the ideal margin for a given node
*
* @param nodeName the parsing node
* @returns the margin bottom as number
*/
private getMarginBottom(nodeName: string): number {
switch (nodeName) {
case 'li': {
return this.LI_MARGIN_BOTTOM;
}
default: {
return this.P_MARGIN_BOTTOM;
}
}
}
/**
* Takes an HTML string, converts to HTML using a DOM parser and recursivly parses
* the content into pdfmake compatible doc definition
@ -154,8 +171,10 @@ export class HtmlToPdfService {
case 'h4':
case 'h5':
case 'h6':
case 'p': {
const children = this.parseChildren(element, newParagraph);
case 'li':
case 'p':
case 'div': {
const children = this.parseChildren(element, styles);
if (this.lineNumberingMode === LineNumberingMode.Outside) {
newParagraph = this.create('stack');
@ -165,15 +184,14 @@ export class HtmlToPdfService {
newParagraph.text = children;
}
newParagraph.margin = [0, this.P_MARGIN_BOTTOM];
newParagraph.margin = [0, this.getMarginBottom(nodeName)];
newParagraph.lineHeight = this.LINE_HEIGHT;
styles = this.computeStyle(styles);
const implicitStyles = this.computeStyle(this.elementStyles[nodeName]);
newParagraph = {
...newParagraph,
...styles,
...this.computeStyle(styles),
...implicitStyles
};
break;
@ -240,19 +258,6 @@ export class HtmlToPdfService {
newParagraph.lineHeight = this.LINE_HEIGHT;
break;
}
case 'li':
case 'div': {
newParagraph = this.create('text');
newParagraph.lineHeight = this.LI_MARGIN_BOTTOM;
newParagraph = {
...newParagraph,
...this.computeStyle(styles)
};
const children = this.parseChildren(element, styles);
newParagraph.text = children;
break;
}
case 'ul':
case 'ol': {
newParagraph = this.create(nodeName);

View File

@ -1155,7 +1155,7 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit {
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
this.pdfExport.exportSingleMotion(this.motion, LineNumberingMode.None, exportCr);
this.pdfExport.exportSingleMotion(this.motion, this.lnMode, exportCr);
}
/**

View File

@ -10,6 +10,7 @@ import { MotionRepositoryService } from 'app/core/repositories/motions/motion-re
import { StatuteParagraphRepositoryService } from 'app/core/repositories/motions/statute-paragraph-repository.service';
import { ViewMotion, LineNumberingMode, ChangeRecoMode } from '../models/view-motion';
import { ViewUnifiedChange } from '../models/view-unified-change';
import { LinenumberingService } from 'app/core/ui-services/linenumbering.service';
/**
* Type declaring which strings are valid options for metainfos to be exported into a pdf
@ -41,6 +42,7 @@ export class MotionPdfService {
* @param configService Read config variables
* @param htmlToPdfService To convert HTML text into pdfmake doc def
* @param pollService MotionPollService for rendering the polls
* @param linenumberingService Line numbers
*/
public constructor(
private translate: TranslateService,
@ -49,7 +51,8 @@ export class MotionPdfService {
private changeRecoRepo: ChangeRecommendationRepositoryService,
private configService: ConfigService,
private htmlToPdfService: HtmlToPdfService,
private pollService: MotionPollService
private pollService: MotionPollService,
private linenumberingService: LinenumberingService
) {}
/**
@ -436,6 +439,8 @@ export class MotionPdfService {
// order of changes applied to the motion
changes.sort((a, b) => a.getLineFrom() - b.getLineFrom());
motionText = this.motionRepo.formatMotion(motion.id, crMode, changes, lineLength);
// reformat motion text to split long HTML elements to easier convert into PDF
motionText = this.linenumberingService.splitInlineElementsAtLineBreaks(motionText);
}
return this.htmlToPdfService.convertHtml(motionText, lnMode);