使用 wkhtmltopdf 库打印时如何接收 DOM 元素的高度?

当我在使用 wkhtmltopdf 库打印时尝试使用 Javascript 获取 offsetHeight 或任何 DOM 元素时,高度永远不会确定并且始终等于 0。当我在任何浏览器中执行相同的 JS 代码时,它可以正常工作并产生特定的高度的元素。

google了半天,发现可能和wkhtmltopdf有关,其中document和window的宽高都为0。 wkhtmltopdf 配置参数,但 offsetHeight 仍然为 0。

使用 wkhtmltopdf 打印时是否有任何已知的解决方法来接收 DOM 元素的高度?

我用的是最新稳定版的打印库(0.12.6)


HUX布斯
浏览 172回答 1
1回答

慕标5832272

我过去使用过 wkHtml2Pdf。我的建议是立即停止,因为 wkhtmltopdf 使用的是非常旧的浏览器版本,无论如何您都可能会遇到问题。此外,wkHtmlToPdf 不能正常工作(而且性能很差)。相反,您可以使用更好的选择。该选项是将 Chrome DevTools 与远程调试协议一起使用:https ://chromedevtools.github.io/devtools-protocol/基本上像这样运行 Chromechrome.exe&nbsp;--remote-debugging-port=9222可选配$"--user-data-dir=\"{directoryInfo.FullName}\"";和"--headless&nbsp;--disable-gpu";这是我在服务器上启动 Chrome 进程的方式(C# 代码)public IChromeProcess Create(int port, bool headless){&nbsp; &nbsp; string path = System.IO.Path.GetRandomFileName();&nbsp; &nbsp; System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(&nbsp; &nbsp; &nbsp; &nbsp; System.IO.Path.Combine(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.IO.Path.GetTempPath(), path)&nbsp; &nbsp; );&nbsp; &nbsp; string remoteDebuggingArg = $"--remote-debugging-port={port}";&nbsp; &nbsp; string userDirectoryArg = $"--user-data-dir=\"{directoryInfo.FullName}\"";&nbsp; &nbsp; const string headlessArg = "--headless --disable-gpu";&nbsp; &nbsp; // https://peter.sh/experiments/chromium-command-line-switches/&nbsp; &nbsp; System.Collections.Generic.List<string> chromeProcessArgs =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; new System.Collections.Generic.List<string>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; remoteDebuggingArg,&nbsp; &nbsp; &nbsp; &nbsp; userDirectoryArg,&nbsp; &nbsp; &nbsp; &nbsp; // Indicates that the browser is in "browse without sign-in" (Guest session) mode.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Should completely disable extensions, sync and bookmarks.&nbsp; &nbsp; &nbsp; &nbsp; "--bwsi",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "--no-first-run"&nbsp; &nbsp; };&nbsp; &nbsp; if (false)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string proxyProtocol = "socks5";&nbsp; &nbsp; &nbsp; &nbsp; proxyProtocol = "http";&nbsp; &nbsp; &nbsp; &nbsp; proxyProtocol = "https";&nbsp; &nbsp; &nbsp; &nbsp; string proxyIP = "68.183.233.181";&nbsp; &nbsp; &nbsp; &nbsp; string proxyPort = "3128";&nbsp; &nbsp; &nbsp; &nbsp; string proxyArg = "--proxy-server=\"" + proxyProtocol + "://" + proxyIP + ":" + proxyPort + "\"";&nbsp; &nbsp; &nbsp; &nbsp; chromeProcessArgs.Add(proxyArg);&nbsp; &nbsp; }&nbsp; &nbsp; if (headless)&nbsp; &nbsp; &nbsp; &nbsp; chromeProcessArgs.Add(headlessArg);&nbsp; &nbsp; if(IsRoot)&nbsp; &nbsp; &nbsp; &nbsp; chromeProcessArgs.Add("--no-sandbox");&nbsp; &nbsp; string args = string.Join(" ", chromeProcessArgs);&nbsp; &nbsp; System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(ChromePath, args);&nbsp; &nbsp; System.Diagnostics.Process chromeProcess = System.Diagnostics.Process.Start(processStartInfo);&nbsp; &nbsp; string remoteDebuggingUrl = "http://localhost:" + port;&nbsp; &nbsp; return new LocalChromeProcess(new System.Uri(remoteDebuggingUrl), () => DirectoryCleaner.Delete(directoryInfo), chromeProcess);}我在这里使用这个 C# 库与 DevTools 交互(通过 WebSockets):https://github.com/MasterDevs/ChromeDevTools如果你在服务器上使用 NodeJS,你可以使用这个:https://github.com/cyrus-and/chrome-remote-interface或者 TypeScript: https://github.com/TracerBench/chrome-debugging-client为了生成 PDF,您需要发出 PrintToPDF 命令:Dim cm2inch As UnitConversion_t = Function(ByVal centimeters As Double) centimeters * 0.393701Dim mm2inch As UnitConversion_t = Function(ByVal milimeters As Double) milimeters * 0.0393701Dim printCommand2 As PrintToPDFCommand = New PrintToPDFCommand() With {&nbsp; &nbsp; .Scale = 1,&nbsp; &nbsp; .MarginTop = 0,&nbsp; &nbsp; .MarginLeft = 0,&nbsp; &nbsp; .MarginRight = 0,&nbsp; &nbsp; .MarginBottom = 0,&nbsp; &nbsp; .PrintBackground = True,&nbsp; &nbsp; .Landscape = False,&nbsp; &nbsp; .PaperWidth = mm2inch(conversionData.PageWidth),&nbsp; &nbsp; .PaperHeight = mm2inch(conversionData.PageHeight) '&nbsp;}要创建光栅图形,您需要发出 CaptureScreenshot-Command :Dim screenshot As MasterDevs.ChromeDevTools.CommandResponse(Of CaptureScreenshotCommandResponse) = Await chromeSession.SendAsync(New CaptureScreenshotCommand With {&nbsp; &nbsp; .Format = "png"})System.Diagnostics.Debug.WriteLine("Screenshot taken.")conversionData.PngData = System.Convert.FromBase64String(screenshot.Result.Data)请注意,要使屏幕截图正常工作,您需要通过 SetDeviceMetricsOverride-Command 设置宽度和高度:Await chromeSession.SendAsync(New SetDeviceMetricsOverrideCommand With {&nbsp; &nbsp; .Width = conversionData.ViewPortWidth,&nbsp; &nbsp; .Height = conversionData.ViewPortHeight,&nbsp; &nbsp; .Scale = 1})您可能必须将 overflow:hidden 放在 HTML 或一些子元素上,这样您就不会截取滚动条;)顺便说一下,如果您需要特定版本的 Windows 版 Chrome(Chromium,因为出于安全原因旧版 Chrome 不可用),您可以从 Chocolatey-Repository 获取它们:https: //chocolatey.org/packages/chromium /#版本历史这是我的完整测试代码供参考(减去一些类)Imports MasterDevs.ChromeDevToolsImports MasterDevs.ChromeDevTools.Protocol.Chrome.BrowserImports MasterDevs.ChromeDevTools.Protocol.Chrome.PageImports MasterDevs.ChromeDevTools.Protocol.Chrome.TargetNamespace Portal_Convert.CdpConverter&nbsp; &nbsp; Public Class ChromiumBasedConverter&nbsp; &nbsp; &nbsp; &nbsp; Private Delegate Function UnitConversion_t(ByVal value As Double) As Double&nbsp; &nbsp; &nbsp; &nbsp; Public Shared Sub KillHeadlessChromes(ByVal writer As System.IO.TextWriter)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim allProcesses As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcesses()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim exeName As String = "\chrome.exe"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If System.Environment.OSVersion.Platform = System.PlatformID.Unix Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exeName = "/chrome"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; For i As Integer = 0 To allProcesses.Length - 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim proc As System.Diagnostics.Process = allProcesses(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim commandLine As String = ProcessUtils.GetCommandLine(proc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If String.IsNullOrEmpty(commandLine) Then Continue For&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; commandLine = commandLine.ToLowerInvariant()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If commandLine.IndexOf(exeName, System.StringComparison.InvariantCultureIgnoreCase) = -1 Then Continue For&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If commandLine.IndexOf("--headless", System.StringComparison.InvariantCultureIgnoreCase) <> -1 Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine($"Killing process {proc.Id} with command line ""{commandLine}""")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessUtils.KillProcessAndChildren(proc.Id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Next&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine($"Finished killing headless chromes")&nbsp; &nbsp; &nbsp; &nbsp; End Sub&nbsp; &nbsp; &nbsp; &nbsp; Public Shared Sub KillHeadlessChromes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KillHeadlessChromes(System.Console.Out)&nbsp; &nbsp; &nbsp; &nbsp; End Sub&nbsp; &nbsp; &nbsp; &nbsp; Private Shared Function __Assign(Of T)(ByRef target As T, value As T) As T&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target = value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return value&nbsp; &nbsp; &nbsp; &nbsp; End Function&nbsp; &nbsp; &nbsp; &nbsp; Public Shared Function KillHeadlessChromesWeb() As System.Collections.Generic.List(Of String)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim ls As System.Collections.Generic.List(Of String) = New System.Collections.Generic.List(Of String)()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Using sw As System.IO.StringWriter = New System.IO.StringWriter(sb)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KillHeadlessChromes(sw)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Using&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Using tr As System.IO.TextReader = New System.IO.StringReader(sb.ToString())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim thisLine As String = Nothing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; While (__Assign(thisLine, tr.ReadLine())) IsNot Nothing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ls.Add(thisLine)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End While&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Using&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.Length = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb = Nothing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return ls&nbsp; &nbsp; &nbsp; &nbsp; End Function&nbsp; &nbsp; &nbsp; &nbsp; Private Shared Async Function InternalConnect(ByVal ci As ConnectionInfo, ByVal remoteDebuggingUri As String) As System.Threading.Tasks.Task&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ci.ChromeProcess = New RemoteChromeProcess(remoteDebuggingUri)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ci.SessionInfo = Await ci.ChromeProcess.StartNewSession()&nbsp; &nbsp; &nbsp; &nbsp; End Function&nbsp; &nbsp; &nbsp; &nbsp; Private Shared Async Function ConnectToChrome(ByVal chromePath As String, ByVal remoteDebuggingUri As String) As System.Threading.Tasks.Task(Of ConnectionInfo)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim ci As ConnectionInfo = New ConnectionInfo()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Await InternalConnect(ci, remoteDebuggingUri)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As System.Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If ex.InnerException IsNot Nothing AndAlso Object.ReferenceEquals(ex.InnerException.[GetType](), GetType(System.Net.WebException)) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (CType(ex.InnerException, System.Net.WebException)).Status = System.Net.WebExceptionStatus.ConnectFailure Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim chromeProcessFactory As MasterDevs.ChromeDevTools.IChromeProcessFactory = New MasterDevs.ChromeDevTools.ChromeProcessFactory(New FastStubbornDirectoryCleaner(), chromePath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim persistentChromeProcess As MasterDevs.ChromeDevTools.IChromeProcess = chromeProcessFactory.Create(9222, True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' await cannot be used inside catch ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' Await InternalConnect(ci, remoteDebuggingUri)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InternalConnect(ci, remoteDebuggingUri).Wait()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return ci&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(chromePath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(ex.Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(ex.StackTrace)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If ex.InnerException IsNot Nothing Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(ex.InnerException.Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(ex.InnerException.StackTrace)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(ex.[GetType]().FullName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throw&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Return ci&nbsp; &nbsp; &nbsp; &nbsp; End Function&nbsp; &nbsp; &nbsp; &nbsp; Private Shared Async Function ClosePage(ByVal chromeSession As MasterDevs.ChromeDevTools.IChromeSession, ByVal frameId As String, ByVal headLess As Boolean) As System.Threading.Tasks.Task&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim closeTargetTask As System.Threading.Tasks.Task(Of MasterDevs.ChromeDevTools.CommandResponse(Of CloseTargetCommandResponse)) = chromeSession.SendAsync(New CloseTargetCommand() With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .TargetId = frameId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' await will block forever if headless&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Not headLess Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim closeTargetResponse As MasterDevs.ChromeDevTools.CommandResponse(Of CloseTargetCommandResponse) = Await closeTargetTask&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(closeTargetResponse)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(closeTargetTask)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; End Function&nbsp; &nbsp; &nbsp; &nbsp; Public Shared Async Function ConvertDataAsync(ByVal conversionData As ConversionData) As System.Threading.Tasks.Task&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim chromeSessionFactory As MasterDevs.ChromeDevTools.IChromeSessionFactory = New MasterDevs.ChromeDevTools.ChromeSessionFactory()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Using connectionInfo As ConnectionInfo = Await ConnectToChrome(conversionData.ChromePath, conversionData.RemoteDebuggingUri)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim chromeSession As MasterDevs.ChromeDevTools.IChromeSession = chromeSessionFactory.Create(connectionInfo.SessionInfo.WebSocketDebuggerUrl)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Await chromeSession.SendAsync(New SetDeviceMetricsOverrideCommand With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Width = conversionData.ViewPortWidth,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Height = conversionData.ViewPortHeight,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Scale = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim navigateResponse As MasterDevs.ChromeDevTools.CommandResponse(Of NavigateCommandResponse) = Await chromeSession.SendAsync(New NavigateCommand With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Url = "about:blank"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine("NavigateResponse: " & navigateResponse.Id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim setContentResponse As MasterDevs.ChromeDevTools.CommandResponse(Of SetDocumentContentCommandResponse) = Await chromeSession.SendAsync(New SetDocumentContentCommand() With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .FrameId = navigateResponse.Result.FrameId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Html = conversionData.Html&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim cm2inch As UnitConversion_t = Function(ByVal centimeters As Double) centimeters * 0.393701&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim mm2inch As UnitConversion_t = Function(ByVal milimeters As Double) milimeters * 0.0393701&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim printCommand2 As PrintToPDFCommand = New PrintToPDFCommand() With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Scale = 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .MarginTop = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .MarginLeft = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .MarginRight = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .MarginBottom = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .PrintBackground = True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Landscape = False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .PaperWidth = mm2inch(conversionData.PageWidth),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .PaperHeight = mm2inch(conversionData.PageHeight) '&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '.PaperWidth = cm2inch(conversionData.PageWidth),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '.PaperHeight = cm2inch(conversionData.PageHeight)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.GetVersion) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("Getting browser-version")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim version As MasterDevs.ChromeDevTools.CommandResponse(Of GetVersionCommandResponse) = Await chromeSession.SendAsync(New GetVersionCommand())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("Got browser-version")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.Version = version.Result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As System.Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.Exception = ex&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine(ex.Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.ConvertToImage) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("Taking screenshot")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim screenshot As MasterDevs.ChromeDevTools.CommandResponse(Of CaptureScreenshotCommandResponse) = Await chromeSession.SendAsync(New CaptureScreenshotCommand With {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Format = "png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("Screenshot taken.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.PngData = System.Convert.FromBase64String(screenshot.Result.Data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As System.Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.Exception = ex&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine(ex.Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.ConvertToPdf) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("Printing PDF")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dim pdf As MasterDevs.ChromeDevTools.CommandResponse(Of PrintToPDFCommandResponse) = Await chromeSession.SendAsync(printCommand2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("PDF printed.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.PdfData = System.Convert.FromBase64String(pdf.Result.Data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Catch ex As System.Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conversionData.Exception = ex&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine(ex.Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine("Closing page")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Await ClosePage(chromeSession, navigateResponse.Result.FrameId, True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine("Page closed")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End Using ' connectionInfo&nbsp; &nbsp; &nbsp; &nbsp; End Function ' ConvertDataAsync&nbsp; &nbsp; &nbsp; &nbsp; Public Shared Sub ConvertData(ByVal conversionData As ConversionData)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConvertDataAsync(conversionData).Wait()&nbsp; &nbsp; &nbsp; &nbsp; End Sub&nbsp; &nbsp; End ClassEnd Namespace请注意,如果有人使用 C#,最好使用此库: https://github.com/BaristaLabs/chrome-dev-tools-runtime,它使用较少的外部依赖项,并且是 NetCore。我使用另一个只是因为我必须将它移植到旧的框架版本......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript