Copied to clipboard

Flag this post as spam?

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


  • Madhav Bhagat 2 posts 72 karma points
    Aug 29, 2016 @ 11:15
    Madhav Bhagat
    0

    Serving generated PDF/vCard from content

    We are developing a site that needs PDF and vCard export of certain document types.

    For PDF,

    We use wkhtmltopdf and followed the instructions at https://github.com/ma1f/umbraco/blob/master/pdf.md.

    However I cannot get the pdf.cshtml file to actually load/run when the user visits say an Article page, but with pdf as the extension (or /pdf as the repo suggests).

    We don't want to use a paid package because it's too expensive and would rather solve this ourselves.

    Any help would be super useful :)

  • Madhav Bhagat 2 posts 72 karma points
    Aug 29, 2016 @ 11:17
    Madhav Bhagat
    0

    The following is the code in the pdf.cshtml file:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Core
    @using Umbraco.Web
    @{
        Layout = null;
        string PDF_CONVERTER = Server.MapPath("/App_data/wkhtmltopdf/wkhtmltopdf.exe");
    
        var filename = Model.Content.UrlName + ".pdf";
        var path = Server.MapPath("/App_Data/pdfs/" + filename);
        var url = Request.Url.Scheme + "://" + Request.Url.Host + Model.Content.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();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft