|
| 1 | +// @flow |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import PDFDocument from 'pdfkit'; |
| 5 | +import qr from 'qr-image'; |
| 6 | +import { MainIpcChannel } from './lib/MainIpcChannel'; |
| 7 | +import { GENERATE_VOTING_PDF_CHANNEL } from '../../common/ipc/api'; |
| 8 | +import type { |
| 9 | + GenerateVotingPDFRendererRequest, |
| 10 | + GenerateVotingPDFMainResponse, |
| 11 | +} from '../../common/ipc/api'; |
| 12 | +import fontRegularEn from '../../common/assets/pdf/NotoSans-Regular.ttf'; |
| 13 | +import fontMediumEn from '../../common/assets/pdf/NotoSans-Medium.ttf'; |
| 14 | +import fontUnicode from '../../common/assets/pdf/arial-unicode.ttf'; |
| 15 | + |
| 16 | +export const generateVotingPDFChannel: // IpcChannel<Incoming, Outgoing> |
| 17 | +MainIpcChannel< |
| 18 | + GenerateVotingPDFRendererRequest, |
| 19 | + GenerateVotingPDFMainResponse |
| 20 | +> = new MainIpcChannel(GENERATE_VOTING_PDF_CHANNEL); |
| 21 | + |
| 22 | +export const handleVotingPDFRequests = () => { |
| 23 | + generateVotingPDFChannel.onReceive( |
| 24 | + (request: GenerateVotingPDFRendererRequest) => |
| 25 | + new Promise((resolve, reject) => { |
| 26 | + // Prepare params |
| 27 | + const { |
| 28 | + title, |
| 29 | + currentLocale, |
| 30 | + creationDate, |
| 31 | + qrCode, |
| 32 | + walletNameLabel, |
| 33 | + walletName, |
| 34 | + isMainnet, |
| 35 | + networkLabel, |
| 36 | + networkName, |
| 37 | + filePath, |
| 38 | + author, |
| 39 | + } = request; |
| 40 | + |
| 41 | + const readAssetSync = (p) => fs.readFileSync(path.join(__dirname, p)); |
| 42 | + let fontRegular; |
| 43 | + let fontMedium; |
| 44 | + |
| 45 | + if (currentLocale === 'ja-JP') { |
| 46 | + fontRegular = fontUnicode; |
| 47 | + fontMedium = fontUnicode; |
| 48 | + } else { |
| 49 | + fontRegular = fontRegularEn; |
| 50 | + fontMedium = fontMediumEn; |
| 51 | + } |
| 52 | + |
| 53 | + // Generate QR image for wallet voting |
| 54 | + const qrCodeImage = qr.imageSync(qrCode, { |
| 55 | + type: 'png', |
| 56 | + size: 10, |
| 57 | + ec_level: 'L', |
| 58 | + margin: 0, |
| 59 | + }); |
| 60 | + |
| 61 | + try { |
| 62 | + const fontBufferMedium = readAssetSync(fontMedium); |
| 63 | + const fontBufferRegular = readAssetSync(fontRegular); |
| 64 | + |
| 65 | + const textColor = '#5e6066'; |
| 66 | + const textColorRed = '#ea4c5b'; |
| 67 | + const width = 640; |
| 68 | + const height = 450; |
| 69 | + const doc = new PDFDocument({ |
| 70 | + size: [width, height], |
| 71 | + margins: { |
| 72 | + bottom: 20, |
| 73 | + left: 30, |
| 74 | + right: 30, |
| 75 | + top: 20, |
| 76 | + }, |
| 77 | + info: { |
| 78 | + Title: title, |
| 79 | + Author: author, |
| 80 | + }, |
| 81 | + }).fillColor(textColor); |
| 82 | + |
| 83 | + // Title |
| 84 | + doc.font(fontBufferMedium).fontSize(18).text(title.toUpperCase(), { |
| 85 | + align: 'center', |
| 86 | + characterSpacing: 2, |
| 87 | + }); |
| 88 | + |
| 89 | + // Creation date |
| 90 | + doc |
| 91 | + .font(fontBufferRegular) |
| 92 | + .fontSize(12) |
| 93 | + .text(creationDate.toUpperCase(), { |
| 94 | + align: 'center', |
| 95 | + characterSpacing: 0.6, |
| 96 | + }); |
| 97 | + |
| 98 | + doc.moveDown(); |
| 99 | + |
| 100 | + // QR Code |
| 101 | + doc.image(qrCodeImage, { |
| 102 | + fit: [width - 60, 192], |
| 103 | + align: 'center', |
| 104 | + }); |
| 105 | + |
| 106 | + doc.moveDown(); |
| 107 | + |
| 108 | + // Wallet name |
| 109 | + doc.font(fontBufferMedium).fontSize(14).text(walletNameLabel, { |
| 110 | + align: 'center', |
| 111 | + characterSpacing: 0.6, |
| 112 | + }); |
| 113 | + doc.font(fontBufferRegular).text(walletName, { |
| 114 | + align: 'center', |
| 115 | + characterSpacing: 0.6, |
| 116 | + }); |
| 117 | + |
| 118 | + doc.moveDown(); |
| 119 | + |
| 120 | + // Footer |
| 121 | + if (!isMainnet) { |
| 122 | + doc |
| 123 | + .fontSize(12) |
| 124 | + .font(fontBufferMedium) |
| 125 | + .fillColor(textColorRed) |
| 126 | + .text(`${networkLabel} ${networkName}`, { |
| 127 | + align: 'center', |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + // Write file to disk |
| 132 | + const writeStream = fs.createWriteStream(filePath); |
| 133 | + doc.pipe(writeStream); |
| 134 | + doc.end(); |
| 135 | + writeStream.on('close', resolve); |
| 136 | + writeStream.on('error', reject); |
| 137 | + } catch (error) { |
| 138 | + reject(error); |
| 139 | + } |
| 140 | + }) |
| 141 | + ); |
| 142 | +}; |
0 commit comments