Skip to content

Commit

Permalink
Merge pull request #342 from ZIMkaRU/feature/improve-print-pdf-under-…
Browse files Browse the repository at this point in the history
…electron

Improve print pdf under electron
  • Loading branch information
ezewer authored Mar 11, 2024
2 parents 630b4a9 + 0c18267 commit ff7588f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"app-builder-bin": "4.2.0",
"cross-env": "7.0.3",
"dotenv": "16.3.1",
"electron": "27.2.0",
"electron": "27.3.5",
"electron-builder": "24.10.0",
"mocha": "10.2.0",
"standard": "17.1.0",
Expand Down
5 changes: 4 additions & 1 deletion src/error-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const manageNewGithubIssue = async (params) => {
}

const initLogger = () => {
log.transports.ipc.level = false
log.transports.console.level = isDevEnv
? 'debug'
: 'warn'
Expand Down Expand Up @@ -215,7 +216,9 @@ const initLogger = () => {
if (
/Cannot download differentially/gi.test(error) ||
/ERR_CONNECTION_REFUSED/gi.test(error) ||
/objects\.githubusercontent\.com/gi.test(error)
/objects\.githubusercontent\.com/gi.test(error) ||
/Error: ERR_FAILED \(-2\) loading 'file:.*\.html'/gi.test(error) ||
/Failed to generate PDF/gi.test(error)
) {
return message
}
Expand Down
40 changes: 24 additions & 16 deletions src/print-to-pdf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const PROCESS_STATES = require(

module.exports = () => {
ipcs.serverIpc.on('message', async (mess) => {
let templateFilePathForRm = null

try {
if (mess?.state !== PROCESS_MESSAGES.REQUEST_PDF_CREATION) {
return
Expand All @@ -33,14 +35,9 @@ module.exports = () => {
templateFilePath &&
typeof templateFilePath === 'string'
)

const html = isTemplateFilePathUsed
? await fs.readFile(templateFilePath, { encoding: 'utf8' })
: template

if (isTemplateFilePathUsed) {
await fs.rm(templateFilePath, { force: true, maxRetries: 3 })
}
templateFilePathForRm = isTemplateFilePathUsed
? templateFilePath
: null

const win = new BrowserWindow({
show: false,
Expand All @@ -49,14 +46,15 @@ module.exports = () => {
nodeIntegration: true
}
})
win.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(html)}`)
const closedEventPromise = new Promise((resolve) => (
win.once('closed', resolve)
))
const loadPromise = isTemplateFilePathUsed
? win.loadFile(templateFilePath)
: win.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(template)}`)

await loadPromise

await new Promise((resolve, reject) => {
win.webContents.on('did-finish-load', resolve)
win.webContents.on('did-fail-load', (e, code, err) => {
reject(err)
})
})
const buffer = await win.webContents.printToPDF({
landscape: format !== 'portrait',
pageSize: orientation,
Expand All @@ -79,7 +77,12 @@ module.exports = () => {
</span>`
})

win.close()
await closedEventPromise

if (isTemplateFilePathUsed) {
await fs.rm(templateFilePath, { force: true, maxRetries: 3 })

const { dir, name } = path.parse(templateFilePath)
const pdfFilePath = path.format({ dir, name, ext: '.pdf' })

Expand All @@ -98,9 +101,14 @@ module.exports = () => {
data: { buffer, uid }
})
} catch (err) {
if (templateFilePathForRm) {
fs.rm(templateFilePathForRm, { force: true, maxRetries: 3 })
.then(() => {}, (err) => { console.debug(err) })
}

ipcs.serverIpc.send({
state: PROCESS_STATES.RESPONSE_PDF_CREATION,
data: { err, uid: mess?.data?.uid ?? null }
data: { err: err.stack ?? err, uid: mess?.data?.uid ?? null }
})

console.error(err)
Expand Down

0 comments on commit ff7588f

Please sign in to comment.