using Syncfusion html to pdf converter, the service is called from the razor page to the controller and then return stream to use javascript at the client side to download the pdf file. This successfully works on my existing Blazor server application but I want to apply the same on a Blazor Wasm shared application.
the challenge now is, the pdf file saves on my wwwroot/Files in my server project but the one that is downloaded on the client side gives error: failed to load pdf document. I'm thinking the problem will come from the return to the client side from the service. But I cant figure out the solution for it.
function in the razor page:
//var exportService = new ExportService();
using (var excelStream = await exportService.CreatePdf(orderHeader))
{
await ToastObjinfo.HideAsync();
await JS.SaveAs(selectedPOHeaderCustomerName + "_OrderNum_" + selectedPOHeaderOrderNumber + ".pdf", excelStream.ToArray());
}
Exportcontroller, in my server project:
[HttpPost()]
[Route("export")]
//Export HTML to PDF document.
public MemoryStream CreatePdf(POHeader orderHeader)
{
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
PdfDocument document = htmlConverter.Convert("https://mymstonline.com/previeworder/" + orderHeader.POHeaderGuid);
FileStream fileStream = new FileStream(@$"wwwroot/Files/" + orderHeader.POHeaderCustomerName + "-OrderNum" + orderHeader.POHeaderOrderNumber + ".pdf", FileMode.Create, FileAccess.Write);
//Save and close the PDF document
document.Save(fileStream);
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);
//and close the Filestream
fileStream.Close();
return stream;
}
and ExportService in my client project:
public class ExportService
{
private readonly HttpClient _httpClient;
public ExportService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<MemoryStream> CreatePdf(POHeader orderHeader)
{
var response = await _httpClient.PostAsJsonAsync("api/Export/export", orderHeader);
var result = await response.Content.ReadAsStreamAsync();
return (MemoryStream)result;
}
}
Our HTML to PDF converter only supported in Blazor server side, currently it is not supported in Blazor WASM.
So, Please use the blazor server side application to perform the HTML To PDF conversion.
Please find the below UG link
https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/blazor