Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Fix pdf export for communication diagrams #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/server/src/main/fonts/dejavuSans.ts

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions packages/server/src/main/resources/diagram-resource.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Request, Response } from 'express';
// @ts-ignore
import pdfMake from 'pdfmake/build/pdfmake.min';
// @ts-ignore
import pdfFonts from 'pdfmake/build/vfs_fonts';
import { DiagramDTO } from '../../../../shared/src/main/diagram-dto';
import { DiagramService } from '../services/diagram-service/diagram-service';
import { DiagramFileStorageService } from '../services/diagram-storage/diagram-file-storage-service';
import { dejavuSans } from '../fonts/dejavuSans';

export class DiagramResource {
diagramService: DiagramService = new DiagramService(new DiagramFileStorageService());
Expand Down Expand Up @@ -45,7 +43,15 @@ export class DiagramResource {
if (width === undefined || height === undefined) {
res.status(400).send('Both width and height must be defined');
} else {
pdfMake.vfs = pdfFonts.pdfMake.vfs;
pdfMake.vfs = dejavuSans;
pdfMake.fonts = {
DejaVuSans: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need a custom font here? is it not possible to solve the issue without a custom fonts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case I would prefer something closer to the default fonts of operating systems such as Helvetica or Arial (in case we can force this, potentially without adding them to the repo)

Copy link
Contributor Author

@mertyldrr mertyldrr Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need a custom font here? is it not possible to solve the issue without a custom fonts?

Apparently, PDF files have 14 standard fonts; Acrobat Reader Base Fonts

These fonts do not support unicode characters, arrows for our case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case I would prefer something closer to the default fonts of operating systems such as Helvetica or Arial (in case we can force this, potentially without adding them to the repo)

We are using normal arrows for up and down, but long arrows for left and right. If we replace long arrows with normal ones, we can use Arial which supports normal arrows. Since Arial is also not supported in pdf, we have to use it as a custom font.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example: Standardized arrows & Arial font

arial

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no other default PDF font which supports the arrow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried all of them, but no luck...

List of fonts supporting arrows

There is a library called pdfmake-unicode that provides Aerial GEO font that supports arrows but this library is not maintained anymore.

normal: 'DejaVuSans.ttf',
bold: 'DejaVuSans.ttf',
italics: 'DejaVuSans.ttf',
bolditalics: 'DejaVuSans.ttf'
},
}
const svg = req.body.svg;
var doc = pdfMake.createPdf({
content: [
Expand All @@ -55,6 +61,9 @@ export class DiagramResource {
],
pageSize: { width, height },
pageMargins: 0,
defaultStyle: {
font: 'DejaVuSans'
}
});
const document = doc.getStream();

Expand Down
Loading