问题在于调用 Playwright.CreateAsync() 方法,这只会永远加载。我最初的评估是因为剧作家想要访问浏览器但无法访问,这就是为什么它只是继续加载。我已经调用了playwright的安装api。
Microsoft.Playwright.Program.Main(new[] { "install", "chromium" });
或
Microsoft.Playwright.Program.Main(new[] { "install"});
正是因为这个方法,下一行代码才没有被执行。
using var playwright = await Playwright.CreateAsync();
这是完整代码
//_logger.LogInformation("Playwright installing.");
Microsoft.Playwright.Program.Main(new[] { "install", "chromium" });
//_logger.LogInformation("Playwright installed.");
_logger.LogInformation("Playwright launching browser.");
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = true,
});
_logger.LogInformation("Playwright browser launched.");
await using var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.SetContentAsync(HTMLString);
// Generate the PDF
var output = await page.PdfAsync(new PagePdfOptions
{
Format = "A4", // or "letter"
Landscape = false,
HeaderTemplate = headerTemplate,
FooterTemplate = footerTemplate,
DisplayHeaderFooter = true,
Margin = new Margin { Top = "0.5in", Bottom = "0.5in", Left = "0.5in", Right = "0.5in" },
});
return output.ToArray();
我发现的其他信息是,在我的本地计算机中,浏览器二进制文件位于
C:\Users\User\AppData\Local\ms-playwright
并且该路径在服务器上不存在。
在azure中运行
microsoft-playwright-run
on-azure-app-service,我们需要将路径更改为下面
"PLAYWRIGHT_BROWSERS_PATH", "value": "/home/site/wwwroot/.playwright/ms-playwright"
_logger.LogInformation("Playwright installing.");
Microsoft.Playwright.Program.Main(new[] { "install", "chromium" });
_logger.LogInformation("Playwright installed.");
_logger.LogInformation("Playwright launching browser.");
// Set PLAYWRIGHT_BROWSERS_PATH to the correct location
Environment.SetEnvironmentVariable("PLAYWRIGHT_BROWSERS_PATH", $"{context.FunctionAppDirectory}\\bin\\");
// Create Playwright instance
using var playwright = await Playwright.CreateAsync();
// Launch browser
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = true,
});
_logger.LogInformation("Playwright browser launched.");
// Create a new browser context and page
await using var browserContext = await browser.NewContextAsync();
var page = await browserContext.NewPageAsync();
// Set HTML content
string HTMLString = "<html><body><h1>Hello, World!</h1></body></html>";
await page.SetContentAsync(HTMLString);
// Generate the PDF
var output = await page.PdfAsync(new PagePdfOptions
{
Format = "A4",
Landscape = false,
DisplayHeaderFooter = true,
Margin = new Margin { Top = "0.5in", Bottom = "0.5in", Left = "0.5in", Right = "0.5in" },
});
使用来自
this参考git的
IronPdf
using IronPdf;
using IronSoftware.IronPdfConsoleDotNetCoreSamples.Infrastructure;
namespace IronSoftware.IronPdfConsoleFrameworkSamples
{
public class DigitalSignatures : IExecuteApp
{
public string OutputPath { get ; set; }
public void Run()
{
// Install IronPdf with Nuget: PM> Install-Package IronPdf
//The quickest way to cryptographically sign an existing PDF a digital certificate
//new IronPdf.PdfSignature("Iron.p12", "123456").SignPdfFile("any.pdf");
// All done in 1 line of code!
//Advanced example for more control
// Step 1. Create a PDF
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
PdfDocument doc = Renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
// Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
// var signature = new IronPdf.PdfSignature(@"Inputs\cert123.pfx", "123");
// Step 3. Optional signing options and a handwritten signature graphic
// signature.SigningContact = "[email protected]";
// signature.SigningLocation = "Chicago, USA";
// signature.SigningReason = "To show how to sign a PDF";
//signature.LoadSignatureImageFromFile(@"Inputs\logo.png");
//Step 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
// doc.SignPdfWithDigitalSignature(signature);
//Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs($@"{OutputPath}\DigitalSignatures.pdf");
}
}
}