PuppeteerSharp PDF随着新一代的发展而变得越来越大

时间:2020-07-17 17:07:39

标签: puppeteer puppeteer-sharp

我正在从HTML生成pdf。如果我在浏览器和SaveAs中加载页面,则所有图片,html,css和js都小于5MB。但是,当我生成PDF时,会生成大量文件,并且它们的大小会不断增加:70MB,90MB,101MB,115MB...。

我正在使用以下代码:

   [HttpPost("PdfByHtml")]
        public async Task<IActionResult> PdfByHtml(PdfByHtml request)
        {
            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = true
            });
            var page = await browser.NewPageAsync();
            await page.EmulateMediaTypeAsync(MediaType.Screen);
            await page.SetContentAsync(request.Html);

            if (request.PdfOptions == null)
            {
                request.PdfOptions = new PdfOptions() { };
            }

            request.PdfOptions.Format = PaperFormat.A4;
            request.PdfOptions.MarginOptions.Bottom = "1.5cm";
            request.PdfOptions.MarginOptions.Top = "1.5cm";
            request.PdfOptions.MarginOptions.Left = "1.5cm";
            request.PdfOptions.MarginOptions.Right = "1.5cm";
            request.PdfOptions.Scale = new decimal(0.8);

            var pdf = File(await page.PdfDataAsync(request.PdfOptions), "application/pdf");
            await browser.CloseAsync();
            return pdf;
        }

版本 PuppeteerSharp 2.0.0 核心2.2

有什么想法吗?

0 个答案:

没有答案