Add image wait, base href and viewport for PDF

This commit is contained in:
√(noham)²
2026-06-19 17:47:29 +02:00
parent 0f0b97e519
commit bcb9c1e767
3 changed files with 13 additions and 9 deletions

View File

@@ -128,15 +128,18 @@ async function main () {
? resolveOutputPath(opts.output) ? resolveOutputPath(opts.output)
: getOutputFilename(title) : getOutputFilename(title)
const contentHtml = await extractRenderedContent( let contentHtml = await extractRenderedContent(readerPage)
readerPage, contentHtml = contentHtml.replace(
`${EXTENSION_BASE}${extId}/data/reader/` '<head>',
`<head><base href="${url}"><meta name="referrer" content="unsafe-url">`
) )
contentHtml = contentHtml.replace(/ loading="lazy"/g, '')
await readerPage.close() await readerPage.close()
const enhancedHtml = enhanceHtml(contentHtml, customCss) const enhancedHtml = enhanceHtml(contentHtml, customCss)
const pdfPage = await browser.newPage() const pdfPage = await browser.newPage()
await pdfPage.setViewport({ width: 1280, height: 7200 })
await generatePdf(pdfPage, enhancedHtml, outputFile) await generatePdf(pdfPage, enhancedHtml, outputFile)
await pdfPage.close() await pdfPage.close()
} finally { } finally {

View File

@@ -43,6 +43,10 @@ export function enhanceHtml (html, customCss) {
export async function generatePdf (page, html, outputPath) { export async function generatePdf (page, html, outputPath) {
await page.setContent(html, { waitUntil: 'networkidle0' }) await page.setContent(html, { waitUntil: 'networkidle0' })
await page.waitForFunction(
() => Array.from(document.images).every(img => img.complete),
{ timeout: 15000, polling: 200 }
).catch(() => logger.debug('Image wait timed out, continuing...'))
logger.info(`Generating PDF: "${outputPath}"`) logger.info(`Generating PDF: "${outputPath}"`)
await page.pdf({ await page.pdf({
path: outputPath, path: outputPath,

View File

@@ -130,8 +130,8 @@ export async function waitForReaderView (page) {
logger.info('Article rendered.') logger.info('Article rendered.')
} }
export async function extractRenderedContent (page, baseUrl) { export async function extractRenderedContent (page) {
const html = await page.evaluate((extBase) => { const html = await page.evaluate(() => {
const iframe = document.querySelector('#content iframe') const iframe = document.querySelector('#content iframe')
if (!iframe) return null if (!iframe) return null
try { try {
@@ -156,14 +156,11 @@ export async function extractRenderedContent (page, baseUrl) {
: inlineVars : inlineVars
} }
const b = doc.createElement('base')
b.href = extBase
doc.head.insertBefore(b, doc.head.firstChild)
return '<!DOCTYPE html>\n' + doc.documentElement.outerHTML return '<!DOCTYPE html>\n' + doc.documentElement.outerHTML
} catch { } catch {
return null return null
} }
}, baseUrl) })
if (!html) throw new Error('Could not extract rendered content from iframe') if (!html) throw new Error('Could not extract rendered content from iframe')
return html return html
} }