英文:
QuestPDF GeneratePdf Process.Start works locally but not when deployed
问题
使用QuestPDF生成PDF文档。在本地使用VS进行调试时,根据Windows设置,它会打开Adobe Acrobat或Chrome。将代码部署到托管的IIS服务器后,执行相同的任务,日志显示所有语句都成功执行,但没有任何内容打开。没有抛出错误,服务器日志也没有显示任何活动。使用生成随机文件名的方法,并尝试了几种不同的路径都没有成功。根据路径设置的方式,使用文件管理器在服务器上或本地都存在该文件。唯一的区别是部署时使用的是发布配置,本地调试时使用的是调试配置。这两种配置都没有进行任何修改。
使用.NET 7、Blazor Server Side、EF Core 7和QuestPDF 2023.5.3。以下是正在使用的代码基础,基本上与文档中的代码相同。
var fileName = $"{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}.pdf";
var filePath = Path.Combine(SpecialDirectories.Temp, fileName); // 本地机器
// var fileName = $@"c:\temp\{fileName}"; // 在服务器上的替代尝试
// var fileName = $@"\fileName"; // 在服务器上的替代尝试
...
... 在此处从数据构建PDF
...
pdf.GeneratePdf(filePath);
Process.Start("explorer.exe", filePath);
// 尝试不同的Process启动方法
// Process.Start("AcroRd32.exe", filePath); // 或
// ProcessStartInfo psi = new()
// {
// FileName = filePath,
// UseShellExecute = true
// };
基本上,在本地运行会打开PDF。这包括使用VS 2022 Ent进行调试,使用发布配置进行调试,以及执行bin发布文件夹中的EXE而不进行调试。提前感谢您的帮助。
更新:我尝试了JSRuntime.InvokeAsync<object>("open", fileName, "_blank")而不是Process.Start,但出现了500 <site><filename>.pdf未找到的错误。不确定这是否是正确的路径,但可能会更接近解决方案。如果有其他方法或使Process.Start、JavaScript调用或其他方法之一的方法,请随时尝试。
英文:
Using QuestPDF to generate PDF documents. When debugging locally using VS it opens Adobe Acrobat or Chrome depending on Windows settings. After deploying the code to the hosting IIS server performing the same tasks, logs show all statements are executed successfully but nothing opens. No errors are thrown and server logs do not show any activity. Using the generate random filename method and tried several different paths with no success. The file does exist when using file manager either on the server or locally depending on which way the path is set. The only difference is deployed is using the release configuration and debugging locally is using the debug configuration. Neither configurations have any modifications.
Using .NET 7, Blazor Server Side, EF Core 7 and QuestPDF 2023.5.3. Here is the basics of the code being used, which is pretty much the same as the docs.
var fileName = $"{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}.pdf";
var filePath = Path.Combine(SpecialDirectories.Temp, fileName); // local machine
// var fileName = $@"c:\temp\{fileName}"; // alternative attempt on server
// var fileName = $@"\fileName"; // alternative attempt on server
...
... build the pdf here from the data
...
pdf.GeneratePdf(filePath);
Process.Start("explorer.exe", filePath);
// try different method for Process start
// Process.Start("AcroRd32.exe", filePath); // or
// ProcessStartInfo psi = new()
// {
// FileName = filePath,
// UseShellExecute = true
// };
Basically running locally opens the PDF. This includes debugging using VS 2022 Ent, starting without debugging using release configuration and executing the EXE in the bin release folder. Thanks in advance.
Update: Instead of the Process.Start I have tried the JSRuntime.InvokeAsync<object>("open", fileName, "_blank"); but am getting 500 <site><filename>.pdf not found. Not sure if this is the right path but may be getting me closer. If there is another way or a way to make either the Process.Start, the JavaScript call or some other way, I am open to giving it a try.
答案1
得分: 0
已成功使其工作,方法如下。
删除:
var fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
... 生成PDF的代码保持不变 ...
Process.Start("explorer.exe", filePath);
添加:
var rootFileName = $"{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}.pdf";
var fileName = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "PDFReports", rootFileName);
var outputFileName = $"PDFReports/{rootFileName}";
... 生成PDF的代码保持不变 ...
await JSRuntime.InvokeVoidAsync("open", outputFileName, "_blank");
这将在wwwroot\PDFReports(fileName)文件夹中生成文件,然后以不带绝对路径的方式打开浏览器标签(outputFolder)。
英文:
Was able to get it to work by doing the following.
Remove
var fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
... Code to generate the PDF remained unchanged ...
Process.Start("explorer.exe", filePath);
Added
var rootFileName = $"{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}.pdf";
var fileName = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "PDFReports", rootFileName);
var outputFileName = $"PDFReports/{rootFileName}";
... Code to generate the PDF remained unchanged ...
await JSRuntime.InvokeVoidAsync("open", outputFileName, "_blank");
This generated the file in the wwwroot\PDFReports (fileName) folder and then opened the browser tab with the file from the same folder without the absolute path (outputFolder).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论