Я попробовал запустить приведенный ниже код, но он выдал ошибку. Ошибка: {"code":-32000, "message": "Печать недоступна". Пожалуйста, предлагайте свои предложения.
public Task PdfDataAsync(PDFPrintConfig1 printConfig)
{
return Task.Run(() =>
{
try
{
// Build CDP parameters for Page.printToPDF
var parameters = new Dictionary
{
["printBackground"] = printConfig.PrintBackground,
["landscape"] = printConfig.Landscape,
["scale"] = Convert.ToDouble(printConfig.Scale),
// paper format: map your FormatString or PaperFormat to width/height if needed
};
var result = ((EdgeDriver)_driver).ExecuteCdpCommand("Page.printToPDF", parameters); // this one fails
// Add margins if required (Chrome expects numbers in inches or maybe values)
// Use default margins; Puppeteer uses strings like "5px" but CDP expects numbers in inches.
// For simplicity we omit margins here.
var printOptions = new PrintOptions
{
Orientation = PrintOrientation.Portrait
};
var pdf = _driver.Print(printOptions); // this one also fails
string pdfBase64 = pdf.AsBase64EncodedString;
byte[] pdfBytes = Convert.FromBase64String(pdfBase64);
if (pdfBytes != null)
return pdfBytes;
_logger.Error("Page.printToPDF returned no data.");
return [];
}
catch (Exception ex)
{
_logger.Error($"PdfDataAsync failed: {ex.Message}");
return [];
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-c-sharp