【问题标题】:Print to pdf using headless chrome on c# ChromeDevTools Application go in breakmode在 c# ChromeDevTools Application 上使用无头 chrome 打印到 pdf 进入中断模式
【发布时间】:2020-10-16 16:39:00
【问题描述】:

我正在尝试使用 Chrome 打印到 PDF 来构建从 HTML 到 PDF 的转换器。选项。

我已经尝试了无头 Chrome 的命令行程序,但我无法添加任何设置,所以我现在尝试使用 MasterDevs.ChromeDevTools

我的问题是应用程序每次通过等待时都会进入中断模式,我真的不明白问题可能是什么。

using System;
using MasterDevs.ChromeDevTools.Protocol.Chrome.Page;
using System.IO;
using System.Linq;
using System.Threading;
using MasterDevs.ChromeDevTools.Protocol.Chrome.DOM;
using Task = System.Threading.Tasks.Task;
using MasterDevs.ChromeDevTools;

namespace ConsoleApp4
{
    internal class Program
    {
        const int ViewPortWidth = 1440;
        const int ViewPortHeight = 900;
        private static void Main(string[] args)
        {
            Task.Run(async () =>
            {


                // STEP 1 - Run Chrome
                var chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());
                using (var chromeProcess = chromeProcessFactory.Create(9222, true))
                {
                    // STEP 2 - Create a debugging session
                    var sessionInfo = (await chromeProcess.GetSessionInfo()).LastOrDefault();
                    var chromeSessionFactory = new ChromeSessionFactory();
                    var chromeSession = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);

                    // STEP 3 - Send a command
                    //
                    // Here we are sending a commands to tell chrome to set the viewport size 
                    // and navigate to the specified URL
                    //await chromeSession.SendAsync(new SetDeviceMetricsOverrideCommand
                    //{
                    //    Width = ViewPortWidth,
                    //    Height = ViewPortHeight,
                    //    Scale = 1
                    //});

                    var navigateResponse = await chromeSession.SendAsync(new NavigateCommand
                    {
                        Url = "http://www.google.com"
                    });
                    Console.WriteLine("NavigateResponse: " + navigateResponse.Id);

                    var printResponse = await chromeSession.SendAsync(new PrintToPDFCommand {
                        Landscape = true
                        , DisplayHeaderFooter = true
                        , MarginBottom = 0
                        , MarginTop = 0
                        , MarginRight = 0
                        , MarginLeft = 0
                    });

                    Console.WriteLine("NavigateResponse: " + navigateResponse.Id);

                    Console.WriteLine("Exiting ..");
                }
            }).Wait();
        }
    }
}

【问题讨论】:

  • 你找到解决办法了吗?
  • @Vaibhav Bhatia:显然不是,但我知道。见答案。

标签: c# google-chrome-headless


【解决方案1】:

好吧,让我告诉你为什么:

您不设置纸张大小或比例!

那么,“MasterDev”没有正确处理Chrome返回的后续错误。

因此,任务永远不会设置为“错误”或“成功”。
这就是为什么在这一点上,C# 一直在等待答案。

你猜我怎么知道的?
1.5年前,我调试了一个小时,遇到了同样的问题;)

这是一个错误修复提案的问题:
https://github.com/MasterDevs/ChromeDevTools/issues/37

Dim cm2inch As UnitConversion_t = Function(ByVal centimeters As Double) centimeters * 0.393701
Dim mm2inch As UnitConversion_t = Function(ByVal milimeters As Double) milimeters * 0.0393701

Dim printCommand2 As PrintToPDFCommand = New PrintToPDFCommand() With {
    .Scale = 1,
    .MarginTop = 0,
    .MarginLeft = 0,
    .MarginRight = 0,
    .MarginBottom = 0,
    .PrintBackground = True,
    .Landscape = False,
    .PaperWidth = mm2inch(conversionData.PageWidth),
    .PaperHeight = mm2inch(conversionData.PageHeight) ' 
}

另请参阅 How to receive the height of DOM element when printing with wkhtmltopdf library?

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2018-02-15
    • 2018-12-24
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多