Skip to content

Commit 3affeba

Browse files
Merge pull request #1574 from RodriSanchez1/fix/textExportPDF
Fix / Support languages that uses characters different than Latin alphabet
2 parents fe22ed2 + 9da3cd0 commit 3affeba

File tree

6 files changed

+194
-85
lines changed

6 files changed

+194
-85
lines changed

src/components/Settings/Export/Export.constants.js

+52
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,58 @@ export const PDF_GRID_BORDER = {
7070
}
7171
};
7272

73+
export const FONTS = {
74+
Roboto: {
75+
normal: 'Roboto-Regular.ttf',
76+
bold: 'Roboto-Regular.ttf'
77+
},
78+
Khmer: {
79+
normal: 'Khmer-Regular.ttf',
80+
bold: 'Khmer-Regular.ttf'
81+
},
82+
Tajawal: {
83+
normal: 'Tajawal-Regular.ttf',
84+
bold: 'Tajawal-Regular.ttf'
85+
},
86+
Sarabun: {
87+
normal: 'Sarabun-Regular.ttf',
88+
bold: 'Sarabun-Regular.ttf'
89+
},
90+
Hind: {
91+
normal: 'Hind-Regular.ttf',
92+
bold: 'Hind-Regular.ttf'
93+
},
94+
NotoSansHebrew: {
95+
normal: 'NotoSansHebrew-Regular.ttf',
96+
bold: 'NotoSansHebrew-Regular.ttf'
97+
},
98+
NotoSansJP: {
99+
normal:
100+
'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansJP-Regular.ttf',
101+
bold:
102+
'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansJP-Regular.ttf'
103+
},
104+
NotoSansKR: {
105+
normal:
106+
'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansKR.otf',
107+
bold: 'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansKR.otf'
108+
},
109+
AnekDevanagari: {
110+
normal: 'AnekDevanagari-Regular.ttf',
111+
bold: 'AnekDevanagari-Regular.ttf'
112+
},
113+
NotoSansSC: {
114+
normal:
115+
'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansSC-Regular.otf',
116+
bold:
117+
'https://cboardgroupqadiag.blob.core.windows.net/fonts/NotoSansSC-Regular.otf'
118+
},
119+
NotoSerifBengali: {
120+
normal: 'NotoSerifBengali-Regular.ttf',
121+
bold: 'NotoSerifBengali-Regular.ttf'
122+
}
123+
};
124+
73125
export const PICSEEPAL_IMAGES_WIDTH = {
74126
column: {
75127
1: 130,

src/components/Settings/Export/Export.container.js

+47-31
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,56 @@ export class ExportContainer extends PureComponent {
3333

3434
const { boards, intl, activeBoardId, showNotification } = this.props;
3535
// TODO: Make this block easier to follow.
36-
if (type === 'openboard' && singleBoard) {
37-
await EXPORT_HELPERS.openboardExportAdapter(singleBoard, intl);
38-
} else if (type === 'cboard') {
39-
await EXPORT_HELPERS.cboardExportAdapter(boards, singleBoard);
40-
} else if (type === 'picsee_pdf') {
41-
if (singleBoard) {
42-
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl, true);
36+
try {
37+
if (type === 'openboard' && singleBoard) {
38+
await EXPORT_HELPERS.openboardExportAdapter(singleBoard, intl);
39+
} else if (type === 'cboard') {
40+
await EXPORT_HELPERS.cboardExportAdapter(boards, singleBoard);
41+
} else if (type === 'picsee_pdf') {
42+
if (singleBoard) {
43+
await EXPORT_HELPERS[exportConfig.callback](
44+
[singleBoard],
45+
intl,
46+
true
47+
);
48+
} else {
49+
const currentBoard = boards.filter(
50+
board => board.id === activeBoardId
51+
);
52+
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl, true);
53+
}
54+
} else if (type !== 'pdf' && !singleBoard) {
55+
await EXPORT_HELPERS[exportConfig.callback](boards, intl);
4356
} else {
44-
const currentBoard = boards.filter(board => board.id === activeBoardId);
45-
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl, true);
57+
if (singleBoard) {
58+
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl);
59+
} else {
60+
const currentBoard = boards.filter(
61+
board => board.id === activeBoardId
62+
);
63+
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl);
64+
}
4665
}
47-
} else if (type !== 'pdf' && !singleBoard) {
48-
await EXPORT_HELPERS[exportConfig.callback](boards, intl);
49-
} else {
50-
if (singleBoard) {
51-
await EXPORT_HELPERS[exportConfig.callback]([singleBoard], intl);
52-
} else {
53-
const currentBoard = boards.filter(board => board.id === activeBoardId);
54-
await EXPORT_HELPERS[exportConfig.callback](currentBoard, intl);
55-
}
56-
}
57-
const showBoardDowloadedNotification = () => {
58-
if (isAndroid())
59-
return showNotification(
60-
intl.formatMessage(messages.boardDownloadedCva)
61-
);
62-
if (isIOS())
63-
return showNotification(
64-
intl.formatMessage(messages.boardDownloadedCvaIOS)
65-
);
66-
return showNotification(intl.formatMessage(messages.boardDownloaded));
67-
};
66+
const showBoardDowloadedNotification = () => {
67+
if (isAndroid())
68+
return showNotification(
69+
intl.formatMessage(messages.boardDownloadedCva)
70+
);
71+
if (isIOS())
72+
return showNotification(
73+
intl.formatMessage(messages.boardDownloadedCvaIOS)
74+
);
75+
return showNotification(intl.formatMessage(messages.boardDownloaded));
76+
};
6877

69-
showBoardDowloadedNotification();
78+
showBoardDowloadedNotification();
79+
} catch (e) {
80+
console.error(e);
81+
const message = e.reason?.message?.startsWith('Failed to fetch')
82+
? messages.downloadNoConnectionError
83+
: messages.boardDownloadError;
84+
showNotification(intl.formatMessage(message));
85+
}
7086
doneCallback();
7187
};
7288

src/components/Settings/Export/Export.helpers.js

+73-42
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
NOT_FOUND_IMAGE,
1616
EMPTY_IMAGE,
1717
PDF_GRID_BORDER,
18+
FONTS,
1819
PICSEEPAL_GRID_WIDTH,
1920
PDF_GRID_WIDTH,
2021
PDF_BORDER_WIDTH,
@@ -39,27 +40,6 @@ import mongoose from 'mongoose';
3940
import * as utils from '../../../components/FixedGrid/utils';
4041

4142
pdfMake.vfs = pdfFonts.pdfMake.vfs;
42-
// Add all supported fonts for languages
43-
pdfMake.fonts = {
44-
Khmer: {
45-
normal: 'Khmer-Regular.ttf',
46-
bold: 'Khmer-Regular.ttf'
47-
},
48-
Roboto: {
49-
normal: 'Roboto-Regular.ttf',
50-
bold: 'Roboto-Medium.ttf',
51-
italics: 'Roboto-Italic.ttf',
52-
bolditalics: 'Roboto-MediumItalic.ttf'
53-
},
54-
Tajawal: {
55-
normal: 'Tajawal-Regular.ttf',
56-
bold: 'Tajawal-Bold.ttf'
57-
},
58-
THSarabunNew: {
59-
normal: 'THSarabunNew.ttf',
60-
bold: 'THSarabunNew.ttf'
61-
}
62-
};
6343

6444
const imageElement = new Image();
6545

@@ -866,21 +846,7 @@ export async function cboardExportAdapter(allBoards = [], board) {
866846
}
867847

868848
export async function pdfExportAdapter(boards = [], intl, picsee = false) {
869-
// change font according to locale
870-
let font = 'Roboto';
871-
switch (intl?.locale) {
872-
case 'km':
873-
font = 'Khmer';
874-
break;
875-
case 'ar':
876-
font = 'Tajawal';
877-
break;
878-
case 'th':
879-
font = 'THSarabunNew';
880-
break;
881-
default:
882-
font = 'Roboto';
883-
}
849+
const font = definePDFfont(intl);
884850

885851
const docDefinition = {
886852
pageSize: 'A4',
@@ -971,16 +937,81 @@ export async function pdfExportAdapter(boards = [], intl, picsee = false) {
971937
}
972938
if (isAndroid() || isIOS()) {
973939
requestCvaWritePermissions();
974-
pdfObj.getBuffer(buffer => {
975-
var blob = new Blob([buffer], { type: 'application/pdf' });
976-
const name = 'Download/' + prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename;
977-
writeCvaFile(name, blob);
978-
});
940+
const getBuffer = callback => {
941+
pdfObj.getBuffer(buffer => {
942+
var blob = new Blob([buffer], { type: 'application/pdf' });
943+
const name =
944+
'Download/' + prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename;
945+
writeCvaFile(name, blob);
946+
callback();
947+
});
948+
};
949+
await generatePDF(getBuffer);
979950
} else {
980951
// On a browser simply use download!
981-
pdfObj.download(prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename);
952+
const dowloadPDF = callback =>
953+
pdfObj.download(prefix + EXPORT_CONFIG_BY_TYPE.pdf.filename, callback);
954+
await generatePDF(dowloadPDF);
955+
}
956+
}
957+
}
958+
959+
//To handle PDF generation errors
960+
function generatePDF(callback) {
961+
return new Promise((resolve, reject) => {
962+
function unhandled(e) {
963+
reject(e);
982964
}
965+
setTimeout(() => {
966+
window.removeEventListener('unhandledrejection', unhandled);
967+
reject(new Error('timeout'));
968+
}, 20000);
969+
window.addEventListener('unhandledrejection', unhandled);
970+
callback(resolve);
971+
});
972+
}
973+
974+
function definePDFfont(intl) {
975+
const pdfFonts = { Roboto: FONTS['Roboto'] };
976+
// change font according to locale
977+
let font = 'Roboto';
978+
switch (intl?.locale) {
979+
case 'km':
980+
font = 'Khmer';
981+
break;
982+
case 'ar':
983+
font = 'Tajawal';
984+
break;
985+
case 'th':
986+
font = 'Sarabun';
987+
break;
988+
case 'hi':
989+
font = 'Hind';
990+
break;
991+
case 'he':
992+
font = 'NotoSansHebrew';
993+
break;
994+
case 'ja':
995+
font = 'NotoSansJP';
996+
break;
997+
case 'ko':
998+
font = 'NotoSansKR';
999+
break;
1000+
case 'ne':
1001+
font = 'AnekDevanagari';
1002+
break;
1003+
case 'zh':
1004+
font = 'NotoSansSC';
1005+
break;
1006+
case 'bn':
1007+
font = 'NotoSerifBengali';
1008+
break;
1009+
default:
1010+
font = 'Roboto';
9831011
}
1012+
pdfFonts[font] = FONTS[font];
1013+
pdfMake.fonts = pdfFonts;
1014+
return font;
9841015
}
9851016

9861017
export default {

src/components/Settings/Export/Export.messages.js

+8
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,13 @@ export default defineMessages({
4040
boards: {
4141
id: 'cboard.components.Settings.Export.boards',
4242
defaultMessage: 'Boards'
43+
},
44+
boardDownloadError: {
45+
id: 'cboard.components.Settings.Export.boardDownloadedError',
46+
defaultMessage: 'Ups..Something went wrong. Please try again'
47+
},
48+
downloadNoConnectionError: {
49+
id: 'cboard.components.Settings.Export.downloadNoConnectionError',
50+
defaultMessage: 'Need internet connection to download the PDF.'
4351
}
4452
});

src/translations/src/cboard.json

+2
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@
443443
"cboard.components.Settings.Export.boardDownloaded": "Your boards were downloaded",
444444
"cboard.components.Settings.Export.boardDownloadedCva": "Your boards were downloaded. Find your file under the downloads folder",
445445
"cboard.components.Settings.Export.boardDownloadedCvaIOS": "Your board was downloaded. Find your file under 'On My device' folder",
446+
"cboard.components.Settings.Export.boardDownloadedError": "Ups..Something went wrong. Please try again",
447+
"cboard.components.Settings.Export.downloadNoConnectionError": "Need internet connection to download the PDF.",
446448
"cboard.components.Settings.Export.boards": "Boards",
447449
"cboard.components.Settings.Import.import": "Import",
448450
"cboard.components.Settings.Import.importSecondary": "This option will import JUST the new boards detected. It WILL NOT import the default boards included on Cboard. Supported formats are {cboardLink} format or {link} format.",

src/vfs_fonts.js

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)