Convert XPS To PDF with XpsToPDF C#

30 Views Asked by At

"I encounter an error while working with the application:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HN28UFN8B020", Request id "0HN28UFN8B020:00000002": An unhandled exception was thrown by the application. System.ArgumentException: StaticResource not found: {StaticResource R0} at PdfSharp.Xps.Parsing.XpsParser.ParseStaticResource[T](String value) at PdfSharp.Xps.Parsing.XpsParser.ParsePathGeometry(String data) at PdfSharp.Xps.Parsing.XpsParser.ParsePath() at PdfSharp.Xps.Parsing.XpsParser.ParseCanvas() at PdfSharp.Xps.Parsing.XpsParser.ParseFixedPage() at PdfSharp.Xps.Parsing.XpsParser.ParseElement() at PdfSharp.Xps.Parsing.XpsParser.Parse() at PdfSharp.Xps.Parsing.XpsParser.Parse(XmlTextReader xmlReader) at PdfSharp.Xps.XpsModel.FixedDocument.GetFixedPage(Int32 index) at PdfSharp.Xps.XpsModel.FixedPageCollection.GetEnumerator()+MoveNext() at PdfSharp.Xps.XpsConverter.Convert(String xpsFilename, String pdfFilename, Int32 docIndex, Boolean createComparisonDocument) at PdfSharp.Xps.XpsConverter.Convert(String xpsFilename, String pdfFilename, Int32 docInde) at Ascon.Pilot.Web.Controllers.FilesController.DownloadPDFFile(Guid id, String position) in D:\Users\Lev\adcm\18\ADCM_Connect\PilotWebApplication\Controllers\DataController.cs:line 722 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication1 application)

For some files, everything converts without problems. But for some, this error occurs. I can't figure out what the reason is and how to fix it. P.S. If there is another working free implementation of this functionality, it can also be considered. Here is my code:

 stream.Seek(0, SeekOrigin.Begin);
 PdfSharp.Pdf.PdfDocument pdfDocument = new PdfSharp.Pdf.PdfDocument();
 string projectFolderPath = AppDomain.CurrentDomain.BaseDirectory;
 string tempXpsFileName = "XPSTemp.xps";
 string tempXpsFilePath = System.IO.Path.Combine(projectFolderPath, tempXpsFileName);

string tempPdfFilePath = System.IO.Path.GetTempFileName();
PdfSharp.Pdf.PdfPage page = pdfDocument.AddPage();
pdfDocument.Save(tempPdfFilePath);

XpsConverter.Convert(tempXpsFilePath, tempPdfFilePath, 0);
MemoryStream pdfStream = new MemoryStream();

using (var fs = new FileStream(tempPdfFilePath, FileMode.Open, FileAccess.Read))
{
   fs.CopyTo(pdfStream);
}
pdfStream.Position = 0;
                            
Response.Headers.Add("Content-Type", "application/pdf");
var encodedFileName = System.Web.HttpUtility.UrlEncode(fileData.name);
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{System.IO.Path.ChangeExtension(encodedFileName, ".pdf")}\"");

return new FileStreamResult(pdfStream, "application/pdf");
0

There are 0 best solutions below