Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Levente Kosa 136 posts 352 karma points
    Nov 11, 2019 @ 10:40
    Levente Kosa
    0

    Hi,

    I'm looking for a solution for generating PDF in Umbraco 8. I found https://wkhtmltopdf.org/ , a lot of people recommend it, although I can't make it work. I created a pdf.cshtml view and added this code (https://our.umbraco.com/forum/umbraco-7/using-umbraco-7/71210-best-and-most-flexible-pdf-generator):

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = null;
        string PDF_CONVERTER = Server.MapPath("/App_data/wkhtmltopdf/wkhtmltopdf.exe");
    
        var filename = Model.Name + ".pdf";
        var path = Server.MapPath("/App_Data/pdfs/" + filename);
        var url = Request.Url.Scheme + "://" + Request.Url.Host + Model.Url;
        // Create directory if needed
        var di = new DirectoryInfo(Path.GetDirectoryName(path));
        if (!di.Exists) { di.Create(); }
    
        // Connect to process
        var process = new System.Diagnostics.Process();
        process.StartInfo.FileName = PDF_CONVERTER;
        process.StartInfo.Arguments = "-T 0 -R 0 -B 0 -L 0 --no-outline --disable-smart-shrinking " + url + " " + "\"" + path + "\"";
        process.Start();
        process.WaitForExit();
    
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + filename);
        Response.ContentType = "application/pdf";
        Response.WriteFile(path);
        Response.Flush();
        Response.End();
    }
    

    When I call the page, two error messages pop up enter image description here enter image description here

    and after the following on the screen:

    Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Users\userrname\source\repos\Project\Project\App_Data\pdfs\Plot 3.pdf'.
    
    Source Error:
    
    
    Line 45:     Response.AddHeader("content-disposition", "attachment; filename=" + filename);
    Line 46:     Response.ContentType = "application/pdf";
    Line 47:     Response.WriteFile(path);
    Line 48:     Response.Flush();
    Line 49:     Response.End();
    
    Source File: C:\Users\username\source\repos\Project\Project\Views\Partials\Components\Plot.cshtml    Line: 47
    

    Any advice?

  • Paul Sterling 718 posts 1534 karma points MVP 8x admin c-trib
    Nov 11, 2019 @ 23:35
    Paul Sterling
    102

    Hi Levente -

    Apologies if I'm stating the obvious here, but the errors looks like you are actually missing the two files mentioned.

    Have you confirmed that those files are present on your web server in the /bin/ folder or installed into the GAC?

    If you do, perhaps you have the wrong package from the wkhtmltopdf distribution? I believe the file libgcc_s_dw2-1.dll is used by the C++ compiler which may indicate you are running the source rather than the compiled version.

    Last thought for you to consider - if this is running on Azure make sure you use the 64-bit distribution...I have seen a similar issue when attempting to use 32-bit assemblies on Azure WebApps.

    -Paul

  • Levente Kosa 136 posts 352 karma points
    Nov 12, 2019 @ 06:02
    Levente Kosa
    0

    Hi Paul,

    Thank you for your reply. I just tried the following on my localhost:

    Now when I call the page it runs and generates the PDF, so that is a success!

  • Dan Soule 33 posts 194 karma points
    Nov 19, 2020 @ 18:55
    Dan Soule
    0

    Hi, I am a noob and trying to do something similar. How are you calling the page? what does the URL look like that passes an HTML page URL to the pdf.cshtml?

  • Graham Davis 110 posts 376 karma points
    Nov 11, 2019 @ 23:40
    Graham Davis
    1

    Hi Levente,

    Trying to run an exe from a web application usually causes a security violation, unless you have full access to the server so you can install the package on the server and assign the permissions that needed to run it from the website. I think this may be the case for you. The error messages lead me to believe that the dll's are either....

    1. missing from the install package
    2. not were the program expects to find them
    3. the server is denying access to them.

    For web applications, online pdf conversions are usually done with a 3rd party dll that is copied to the bin folder of the web app. I use SautinSoft for my pdfs.

    https://www.sautinsoft.com/products/pdf-vision/index.php

    The code to create it looks like this:

    SautinSoft.PdfVision _pdf = new SautinSoft.PdfVision();
    

    _pdf.PageStyle.PageMarginRight.Inch(float.Parse("0.5")); _pdf.PageStyle.PageMarginTop.Inch(float.Parse("0.0")); _pdf.PageStyle.PageMarginBottom.Inch(float.Parse("0.5")); _pdf.PageStyle.PageOrientation.Portrait(); _pdf.PageStyle.PageMarginLeft.Inch(float.Parse("0.5")); _pdf.ConvertHtmlStringToPDFFile(html, fileName);

Please Sign in or register to post replies

Write your reply to:

Draft