【发布时间】: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:显然不是,但我知道。见答案。