Copied to clipboard

Flag this post as spam?

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


  • Dan Soule 22 posts 102 karma points
    Nov 05, 2020 @ 18:12
    Dan Soule
    0

    PDF Generation from CMS data

    I am migrating from v7 to v8 and had used Moriyama PDF Creator to generate PDF documents from CMS data. Unfortunately this package is no longer available and does not work on Umbraco v8. What are other people using on Umbraco v8 to generate PDF documents from CMS data? (PS: I am deployed on Microsoft ISS for my web server.)

  • Nigel Wilson 939 posts 2061 karma points
    Nov 05, 2020 @ 20:25
    Nigel Wilson
    0

    Hi Dan

    I built a site a few years back (V7) and used iTextSharp for generating PDF's.

    Admittedly all the generation code had to be written from scratch to suit the requirements, but in essence, the same code would quite easily port over to V8 as it used IPublishedContent objects.

    To download the file I used a file handler that took a couple of query string parameters.

    Not sure it would be of any use, but more than happy to share the code I have if you need something to kick start the coding (assuming you are a coder).

    Cheers

    Nigel

  • Dan Soule 22 posts 102 karma points
    Nov 06, 2020 @ 00:07
    Dan Soule
    0

    thanks Nigel, I am not sure if this is the way I want to go. I can code (HTML, CSS, Javascript, XML-FO) but not sure how much coding is needed to go this route. So I would appreciate a look at your code. Thanks.

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 16, 2020 @ 09:11
  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:30
    Tim Geyssens
    0

    if you are on your own webserver you can just use chrome.exe to gen a pdf of the html page... using the print css...

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:31
    Tim Geyssens
    0

    something like:

     public class ChromeController : ApiController
    {
        // http://localhost:53517/api/Chrome?url=https://www.google.nl
        public HttpResponseMessage Get(string url)
        {
            var outputDirectory = WebConfigurationManager.AppSettings["PdfTempStorage"];
            bool chromeLogging = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableChromeLogging"]);
    
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
    
            var pdfResult = ChromePdfRenderer.RenderPdf(url, outputDirectory, enableLogging: chromeLogging);
    
            HttpResponseMessage result = new HttpResponseMessage();
    
            if (pdfResult.Result == RenderResult.Success)
            {
                result.StatusCode = HttpStatusCode.OK;
                result.Content = new ByteArrayContent(File.ReadAllBytes(pdfResult.File));
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            }
            else
            {
                result.StatusCode = HttpStatusCode.InternalServerError;
                result.Content = new StringContent("Error");
            }
    
            return result;
        }
    }
    
  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:32
    Tim Geyssens
    0

    and the chromerender class is

    public static class ChromePdfRenderer
    {
        public static PdfRenderResult RenderPdf(string url, string outputDirectory, int renderTimeout = 20000, bool enableLogging = false)
        {
            var basePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    
            var outputFile = Path.Combine(outputDirectory, Guid.NewGuid() + ".pdf");
    
            string arguments;
            if (enableLogging)
            {
                arguments = "--headless --enable-logging --v=1 --disable-gpu --no-margins --print-to-pdf=" + outputFile + " " + url;
            }
            else
            {
                arguments = "--headless --disable-gpu --no-margins --print-to-pdf=" + outputFile + " " + url;
            }
    
            var startInfo = new ProcessStartInfo();
            startInfo.FileName = basePath;
            startInfo.Arguments = arguments;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = false;
            startInfo.UseShellExecute = false;
    
            var result = new PdfRenderResult();
            var watch = Stopwatch.StartNew();
            var process = Process.Start(startInfo);
    
            //result.Log = process.StandardOutput.ReadToEnd();
            process.WaitForExit(renderTimeout);
    
            if (!process.HasExited)
            {
                process.Kill();
                result.Result = RenderResult.Error;
            }
    
            result.ExitCode = process.ExitCode;
    
            if (result.ExitCode == 0)
            {
                result.Result = RenderResult.Success;
                result.File = outputFile;
            }
            else
            {
                result.Result = RenderResult.Error;
            }
    
            process.Dispose();
            watch.Stop();
            result.Runtime = watch.ElapsedMilliseconds;
    
            return result;
        }
    }
    
  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:32
    Tim Geyssens
    0

    basepath needs to be correct of course.. but if you have access to the server you can check that var basePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:33
    Tim Geyssens
    1

    so then you just have an api you can use that get's a url and outputs a pdf (using print style of the page)

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 05, 2020 @ 20:35
    Tim Geyssens
    0

    no dependency on any cms (version)... so doesn't break when ugrading

  • Dan Soule 22 posts 102 karma points
    Nov 06, 2020 @ 00:10
    Dan Soule
    0

    Thanks Tim, I will look into this. I am on my own server (albeit a VM) so I do have access to file system and executables. Thanks again.

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 06, 2020 @ 08:36
    Tim Geyssens
    0

    sure should be doable on a VM... we do the same...

  • Ulf Möllerström 46 posts 190 karma points
    Nov 11, 2020 @ 08:29
    Ulf Möllerström
    0

    Tim, Haven't had time to test on a server, but does this need to have any user settings (it, Chrome that is, is supposed to run in a user context)?

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Nov 11, 2020 @ 08:31
    Tim Geyssens
    1

    nope.. it is run --headless , just piping the params to the .exe and getting the result

  • jonok 260 posts 476 karma points
    Nov 06, 2020 @ 03:52
    jonok
    0

    Hi Dan, I've set this up using iTextSharp on multiple v7 sites and the code should work fine on v8 with a few syntax tweaks. Happy to share the code with you if you need.

  • Dan Soule 22 posts 102 karma points
    Nov 06, 2020 @ 16:53
    Dan Soule
    0

    Thanks jonok, I like what I am seeing, and would love it if you could share your code. my email is [email protected]

  • Ulf Möllerström 46 posts 190 karma points
    Nov 16, 2020 @ 10:44
    Ulf Möllerström
    100

    I've just implemented pdf-generation with SelectPdf.HtmlToPdf (it's for free now) and its about 4 lines of code.

    var converter = new SelectPdf.HtmlToPdf();
    var doc = converter.ConvertUrl(uri.ToString());
    var bytes = doc.Save();
    doc.Close();
    
  • Dan Soule 22 posts 102 karma points
    Nov 24, 2020 @ 03:31
    Dan Soule
    0

    I tried several of the solutions that the community suggested including Chrome2HTML, iTextSharp and wkhtmltopdf. I got all three of these to work, but found the results of converting an HTML page to a PDF lacked much of the fine grained control that I was accustom to using XML-FO and PDF Creator. After doing some research I found that PDF Creator was based on the ibex40 engine. I then disassembled the Moriyama PDF Creator package and manually installed the components and templates per the packing list into the Umbraco 8 structure. This seemed to work, leading me to think that the only problem with Moriyama PDF Creator in Umbraco 8 is the packaging and deployment list.

    I want to Thank the community for all the great help

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies