Этот пример я нашел в Интернете мне помогло:
Код: Выделить всё
MemoryStream stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
// Add some text to the PDF
document.Add(new Paragraph("Hello, world!"));
// Close the document
document.Close();
// Create a copy of the MemoryStream
MemoryStream copyStream = new MemoryStream(stream.ToArray());
// Save the PDF to the Downloads folder
string fileName = "myPDF.pdf";
var downloadsPath = Path.Combine(FileSystem.CacheDirectory, "Downloads");
Directory.CreateDirectory(downloadsPath);
string filePath = Path.Combine(downloadsPath, fileName);
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
await copyStream.CopyToAsync(fileStream);
}
// Launch the file using the FileLauncher
await Launcher.OpenAsync(new OpenFileRequest
{
File = new ReadOnlyFile(filePath)
});
Код: Выделить всё
MemoryStream stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
// Add some HTML content to the PDF
string html = "Hello, world!";
using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
{
HtmlConverter.ConvertToPdf(htmlStream, pdfDoc);
}
// Close the document
document.Close();
// Create a copy of the MemoryStream
MemoryStream copyStream = new MemoryStream(stream.ToArray());
// Save the PDF to the Downloads folder
string fileName = "myPDF.pdf";
var downloadsPath = Path.Combine(FileSystem.CacheDirectory, "Downloads");
Directory.CreateDirectory(downloadsPath);
string filePath = Path.Combine(downloadsPath, fileName);
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
await copyStream.CopyToAsync(fileStream);
}
// Launch the file using the FileLauncher
await Launcher.OpenAsync(new OpenFileRequest
{
File = new ReadOnlyFile(filePath)
});
выдает мне эту ошибку :
System.UriFormatException: «Неверный URI: формат URI не может быть определен».
Как мне это сделать? Возможно, я потерялся в своем коде и ошибаюсь при преобразовании в PDF из строки html. Я сделал это просто, потому что собираюсь получить большой HTML-файл с другого сервера и позже установить его в переменную.
Подробнее здесь: https://stackoverflow.com/questions/756 ... p-net-maui