Search code examples
blazorblazor-server-side

QuestPDF GeneratePdf Process.Start works locally but not when deployed


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("open", fileName, "_blank"); but am getting 500 <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.


Solution

  • 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).