Question

I builded an application to automatically generate PDF, taking data from a DB, but recently the customer noticed that the images were rendered different, using RGB color profile, instead of CMYK.

So I tried to build a simple test code, in order to find what could be the problem, but the result was the same.

Here my example code:

controller

namespace PdfTest.Controllers
{
    public class HomeController : Controller
    {
        private string _license;

        public HomeController()
        {
            _license = ConfigurationManager.AppSettings["abcPDFLicense"];
            XSettings.InstallLicense(_license);
        }

        public PartialViewResult Index()
        {
            using (Doc doc = new Doc())
            {
                doc.HtmlOptions.Engine = EngineType.Gecko;
                doc.HtmlOptions.AddLinks = true;

                string html = RenderRazorViewToString("Index", "");
                int pageId = doc.AddImageHtml(html, true, 2500, true);

                while (doc.Chainable(pageId))
                {
                    doc.Page = doc.AddPage();
                    pageId = doc.AddImageToChain(pageId);
                }

                for (int i = 1; i <= doc.PageCount; i++)
                {
                    doc.PageNumber = i;
                    doc.Flatten();
                }

                string pdfDirectory = Server.MapPath("~/PDF_Diesel");
                DirectoryInfo directoryInfo = new DirectoryInfo(pdfDirectory);

                if (!directoryInfo.Exists)
                    directoryInfo.Create();

                string fileName = "test.pdf";

                doc.Rendering.ColorSpace = XRendering.ColorSpaceType.Cmyk;

                doc.Save(string.Format(@"{0}\{1}", directoryInfo.FullName, fileName));
                doc.Clear();
            }

            return PartialView();
        }

        public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;

            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

                return sw.GetStringBuilder().ToString();
            }
        }
    }
}

view

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <img src="file://@Server.MapPath("~/Images/_banner2.jpg")" />
    <img src="~/Images/_banner2.jpg" />
</body>
</html>

This code returns me a view with the loaded image and a sample pdf with that image only, but still continue to render the image in RGB, not in CMYK.

I read ABCpdf docs, but I didn't found the solution of my problem, does anyone knows what could be the issue?

Was it helpful?

Solution

Ok, I figured out what was the issue.

I didn't know that HTML doesn't render CMYK color profile, but only RGB.

To solve the problem I changed all the images color profile with photoshop from CMYK to S_RGB, without losing so much quality of the colors, thankfully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top