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 33 posts 194 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 944 posts 2076 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 33 posts 194 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.

  • Comment author was deleted

    Nov 16, 2020 @ 09:11
  • Comment author was deleted

    Nov 05, 2020 @ 20:30

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

  • Comment author was deleted

    Nov 05, 2020 @ 20:31

    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;
        }
    }
    
  • Comment author was deleted

    Nov 05, 2020 @ 20:32

    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;
        }
    }
    
  • Comment author was deleted

    Nov 05, 2020 @ 20:32

    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";

  • Comment author was deleted

    Nov 05, 2020 @ 20:33

    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)

  • Comment author was deleted

    Nov 05, 2020 @ 20:35

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

  • Dan Soule 33 posts 194 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.

  • Comment author was deleted

    Nov 06, 2020 @ 08:36

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

  • Ulf Möllerström 69 posts 246 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)?

  • Comment author was deleted

    Nov 11, 2020 @ 08:31

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

  • jonok 297 posts 658 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 33 posts 194 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 69 posts 246 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 33 posts 194 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

  • Osman Coskun 164 posts 398 karma points
    May 31, 2023 @ 06:11
    Osman Coskun
    100

    Hello Dan, I'm rebuilding an Umbraco 7 website in v8. We had purchased PDF Creator license for v7 site. I want to use PDF Creator with v8. Can you please guide me how you managed PDF Creator work? When i tried to install package via backoffice, installation stopped with error. Then i checked the package contents and project files i see all the contents are present in project. When i try to generate pdf i only get an xml output.

    Edit: I added the following into system.webserver in web.config, it works now.

    <add name="PdfModule" type="FergusonMoriyama.Pdf.Application.Module.PdfModule, FergusonMoriyama.Pdf" />
    
  • Dan Soule 33 posts 194 karma points
    May 31, 2023 @ 15:22
    Dan Soule
    0

    Glad you got it working... looking for someone who can migrate this package to Umbraco 9-12 as a dot Net package.

Please Sign in or register to post replies

Write your reply to:

Draft