Reputation: 997
I am trying to receive a base64 string as a PDF and print it out.
I have this code:
public void PrintPdfFromBase64(string base64String, string printerName)
{
byte[] pdfBytes = Convert.FromBase64String(base64String);
string tempFilePath = Path.GetTempFileName() + ".pdf";
File.WriteAllBytes(tempFilePath, pdfBytes);
try
{
using (var pdfStream = new MemoryStream(pdfBytes))
{
using (var document = PdfiumViewer.PdfDocument.Load(pdfStream))
{
var printDocument = document.CreatePrintDocument();
printDocument.PrinterSettings = new PrinterSettings
{
PrinterName = printerName
};
printDocument.PrinterSettings.Copies = 1;
printDocument.DefaultPageSettings.Landscape = false;
printDocument.Print();
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while printing: {ex.Message}");
}
finally
{
// Clean up the temporary file
File.Delete(tempFilePath);
}
}
I installed pdfium nuget package, together with these:
However, when I send a print job it crashes with:
Could not load file or assembly 'System.Windows.Forms'
I am running an ASP.NET Core 8 app.
Has anyone had any experience with this?
Upvotes: 1
Views: 55