Skip to content

Instantly share code, notes, and snippets.

@jraps20
Created October 24, 2017 03:49
Show Gist options
  • Save jraps20/4fb576768ec97029d31f873df739c4f5 to your computer and use it in GitHub Desktop.
Save jraps20/4fb576768ec97029d31f873df739c4f5 to your computer and use it in GitHub Desktop.
Gist to illustrate saving a PNG generated via Sitecore PhantomJS as a PDF.
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using Sitecore.ContentTesting.Pipelines.GetScreenShotForURL;
namespace MyLibrary.GetScreenShotForURL
{
public class GeneratePdf : GenerateScreenShotProcessor
{
public override void Process(GetScreenShotForURLArgs args)
{
var imagePath = Sitecore.ContentTesting.Helpers.PathHelper.MapPath(args.OutputFilename);
var pdfPath = imagePath.Replace(".png", ".pdf");
using (var doc = new PdfDocument())
{
var img = XImage.FromFile(imagePath);
var page = new PdfPage
{
Height = img.PointHeight,
Width = img.PointWidth
};
doc.Pages.Add(page);
var xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0);
doc.Save(pdfPath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment