Skip to content

Commit

Permalink
Merge pull request #551 from marp-team/bugfix-large-file-conversion
Browse files Browse the repository at this point in the history
Render HTML via `Page.setContent()` instead of data URI
  • Loading branch information
yhatt authored Sep 23, 2023
2 parents faded9f + 47f0244 commit d82fd0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- Upgrade development Node.js LTS to v18.18.0 ([#547](https://github.com/marp-team/marp-cli/pull/547))
- Upgrade dependent packages to the latest versions ([#548](https://github.com/marp-team/marp-cli/pull/548))

### Fixed

- A huge document fails generating PDF/PPTX/images by `net::ERR_ABORTED` ([#545](https://github.com/marp-team/marp-cli/issues/545), [#551](https://github.com/marp-team/marp-cli/pull/551))

## v3.2.1 - 2023-08-24

### Added
Expand Down
43 changes: 27 additions & 16 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { URL } from 'url'
import type { Marp, MarpOptions } from '@marp-team/marp-core'
import { Marpit, Options as MarpitOptions } from '@marp-team/marpit'
import chalk from 'chalk'
import type { Browser, Page, HTTPRequest } from 'puppeteer-core'
import type { Browser, Page, HTTPRequest, WaitForOptions } from 'puppeteer-core'
import { silence, warn } from './cli'
import { Engine, ResolvedEngine } from './engine'
import { generateOverrideGlobalDirectivesPlugin } from './engine/directive-plugin'
Expand Down Expand Up @@ -297,8 +297,8 @@ export class Converter {

let outlineData: OutlineData | undefined

ret.buffer = await this.usePuppeteer(html, async (page, uri) => {
await page.goto(uri, { waitUntil: ['domcontentloaded', 'networkidle0'] })
ret.buffer = await this.usePuppeteer(html, async (page, { render }) => {
await render()

if (tpl.rendered.outline) {
outlineData = await page.evaluate(
Expand Down Expand Up @@ -384,34 +384,29 @@ export class Converter {

const files: File[] = []

await this.usePuppeteer(html, async (page, uri) => {
await this.usePuppeteer(html, async (page, { render }) => {
await page.setViewport({
...tpl.rendered.size,
deviceScaleFactor: opts.scale ?? 1,
})
await page.goto(uri, { waitUntil: ['domcontentloaded', 'networkidle0'] })
await render()
await page.emulateMediaType('print')

const screenshot = async (pageNumber = 1) => {
// for Chrome < 89 (TODO: Remove this script evaluation in future)
await page.evaluate(
`window.scrollTo(0,${(pageNumber - 1) * tpl.rendered.size.height})`
)

const clip = {
x: 0,
y: (pageNumber - 1) * tpl.rendered.size.height,
...tpl.rendered.size,
} as const

if (opts.type === ConvertType.jpeg)
return (await page.screenshot({
return await page.screenshot({
clip,
quality: opts.quality,
type: 'jpeg',
})) as Buffer
})

return (await page.screenshot({ clip, type: 'png' })) as Buffer
return await page.screenshot({ clip, type: 'png' })
}

if (opts.pages) {
Expand Down Expand Up @@ -532,7 +527,10 @@ export class Converter {

private async usePuppeteer<T>(
baseFile: File,
processer: (page: Page, uri: string) => Promise<T>
processer: (
page: Page,
helpers: { render: () => Promise<void> }
) => Promise<T>
) {
const tmpFile: File.TmpFileInterface | undefined = await (() => {
if (!this.options.allowLocalFiles) return undefined
Expand Down Expand Up @@ -563,7 +561,7 @@ export class Converter {
}
return `file://${tmpFile.path}`
}
return `data:text/html;base64,${baseFile.buffer!.toString('base64')}`
return undefined
})()

const page = await browser.newPage()
Expand All @@ -572,8 +570,21 @@ export class Converter {
const { missingFileSet, failedFileSet } =
this.trackFailedLocalFileAccess(page)

const render = async () => {
const waitForOptions: WaitForOptions = {
waitUntil: ['domcontentloaded', 'networkidle0'],
}

if (uri) {
await page.goto(uri, waitForOptions)
} else {
await page.goto('data:text/html,')
await page.setContent(baseFile.buffer!.toString(), waitForOptions)
}
}

try {
return await processer(page, uri)
return await processer(page, { render })
} finally {
if (missingFileSet.size > 0) {
warn(
Expand Down

0 comments on commit d82fd0f

Please sign in to comment.