public async void Generate_smoke()
        {
            var file = TestConfig.PathToDocumentPdf;
            CreateImageFromPdfTask sut = new CreateImageFromPdfTask();

            var convertParams = new CreatePdfImageTaskParams()
            {
                Dpi = 150,
                FromPage = 1,
                Pages = 1,
                Format = CreatePdfImageTaskParams.ImageFormat.Jpg,
            };
            Boolean wasCalled = false;
            var result = await sut.Run(
                file,
                convertParams,
                (i, s) =>
                {
                    wasCalled = true;
                    return Task.FromResult<Boolean>(true);
                }
            );

            Assert.IsTrue(wasCalled, "conversion failed.");

        }
        protected async override Task<ProcessResult> OnPolling(PollerJobParameters parameters, string workingFolder)
        {
            String format = parameters.All[JobKeys.ThumbnailFormat];

            Logger.DebugFormat("Conversion for jobId {0} in format {1} starting", parameters.JobId, format);

            var task = _taskFactory();
            var passwords = ClientPasswordSet.GetPasswordFor(parameters.FileName);
            foreach (var password in passwords)
            {
                task.Passwords.Add(password);
            }
            string pathToFile = await DownloadBlob(parameters.TenantId, parameters.JobId, parameters.FileName, workingFolder);

            var convertParams = new CreatePdfImageTaskParams()
            {
                Dpi = parameters.GetIntOrDefault(JobKeys.Dpi, 150),
                FromPage = parameters.GetIntOrDefault(JobKeys.PagesFrom, 1),
                Pages = parameters.GetIntOrDefault(JobKeys.PagesCount, 1),
                Format = (CreatePdfImageTaskParams.ImageFormat)Enum.Parse(typeof(CreatePdfImageTaskParams.ImageFormat), format, true)
            };

            await task.Run(
                pathToFile,
                convertParams,
                (i, s) => Write(workingFolder, parameters, format, i, s) //Currying
            );


            Logger.DebugFormat("Conversion of {0} in format {1} done", parameters.JobId, format);
            return ProcessResult.Ok;
        }
        public async void Generate_with_password_removal()
        {
            var file = TestConfig.PathToPasswordProtectedPdf;
            CreateImageFromPdfTask sut = new CreateImageFromPdfTask();
            sut.Decryptor = new PdfDecrypt() {Logger = new TestLogger()};
            sut.Passwords.Add("jarvistest");

            var convertParams = new CreatePdfImageTaskParams()
            {
                Dpi = 150,
                FromPage = 1,
                Pages = 1,
                Format = CreatePdfImageTaskParams.ImageFormat.Jpg,
            };
            Boolean wasCalled = false;
            var result = await sut.Run(
                file,
                convertParams,
                (i, s) =>
                {
                    wasCalled = true;
                    return Task.FromResult<Boolean>(true);
                }
            );

            Assert.IsTrue(wasCalled, "conversion failed.");

        }
Example #4
0
        protected async override Task <ProcessResult> OnPolling(PollerJobParameters parameters, string workingFolder)
        {
            //if this handle already has rasterImage we do not need to do anything
            //we can skip only if this action is not forced or if the pdf was not generated by office
            //because office file can be re-generated due to errors in conversoin (libreoffice or MsOffice)
            if (!IsForced(parameters) && !FromPipelineId(parameters, "office"))
            {
                var formats = GetFormats(parameters.TenantId, parameters.JobId);
                if (formats.Any(f => f == DocumentFormats.RasterImage))
                {
                    return(ProcessResult.Ok);
                }
            }

            String format = parameters.All[JobKeys.ThumbnailFormat];

            Logger.DebugFormat("Conversion for jobId {0} in format {1} starting", parameters.JobId, format);

            var task      = _taskFactory();
            var passwords = ClientPasswordSet.GetPasswordFor(parameters.FileName);

            foreach (var password in passwords)
            {
                task.Passwords.Add(password);
            }
            string pathToFile = await DownloadBlob(parameters.TenantId, parameters.JobId, parameters.FileName, workingFolder).ConfigureAwait(false);

            var convertParams = new CreatePdfImageTaskParams()
            {
                Dpi      = parameters.GetIntOrDefault(JobKeys.Dpi, 150),
                FromPage = parameters.GetIntOrDefault(JobKeys.PagesFrom, 1),
                Pages    = parameters.GetIntOrDefault(JobKeys.PagesCount, 1),
                Format   = (CreatePdfImageTaskParams.ImageFormat)Enum.Parse(typeof(CreatePdfImageTaskParams.ImageFormat), format, true)
            };

            await task.Run(
                pathToFile,
                convertParams,
                (i, s) => Write(workingFolder, parameters, format, i, s) //Currying
                ).ConfigureAwait(false);

            Logger.DebugFormat("Conversion of {0} in format {1} done", parameters.JobId, format);
            return(ProcessResult.Ok);
        }
 private static void TestFile(
     List<PollerTestResult> retValue,
     CreateImageFromPdfTask task,
     String fileName,
     Byte[] fileContent)
 {
     var tempFile = Path.Combine(Path.GetTempPath(), fileName);
     if (File.Exists(tempFile)) File.Delete(tempFile);
     File.WriteAllBytes(tempFile, fileContent);
     try
     {
         var convertParams = new CreatePdfImageTaskParams()
         {
             Dpi = 150,
             FromPage = 1,
             Pages = 1,
             Format = CreatePdfImageTaskParams.ImageFormat.Jpg,
         };
         Boolean wasCalled = false;
         var result = task.Run(
             tempFile,
             convertParams,
             (i, s) =>
             {
                 wasCalled = true;
                 return Task.FromResult<Boolean>(true);
             }
         );
         result.Wait();
         if (wasCalled)
         {
             retValue.Add(new PollerTestResult(true, "Pdf to Jpg"));
         }
         else
         {
             retValue.Add(new PollerTestResult(false, "Pdf to Jpg"));
         }
     }
     catch (Exception ex)
     {
         retValue.Add(new PollerTestResult(false, "Pdf to Jpg: " + ex.Message));
     }
 }
        public async Task <Boolean> Run(
            String pathToFile,
            CreatePdfImageTaskParams createPdfImageTaskParams,
            Func <int, Stream, Task <Boolean> > pageWriter)
        {
            String tempFileName = null;

            if (Passwords.Count > 0)
            {
                tempFileName =
                    Path.Combine(Path.GetDirectoryName(pathToFile),
                                 Path.GetFileNameWithoutExtension(pathToFile) + "_decrypted.pdf");
                if (Decryptor.DecryptFile(pathToFile, tempFileName, Passwords))
                {
                    pathToFile = tempFileName;
                }
            }
            using (var sourceStream = File.OpenRead(pathToFile))
            {
                var settings = new MagickReadSettings
                {
                    Density = new PointD(createPdfImageTaskParams.Dpi, createPdfImageTaskParams.Dpi)
                };
                settings.FrameIndex = 0; // First page
                settings.FrameCount = 1; // Number of pages
                MagickFormat imageFormat = TranslateFormat(createPdfImageTaskParams.Format);

                Logger.DebugFormat("Image format is {0}", imageFormat.ToString());
                using (var images = new MagickImageCollection())
                {
                    bool done = false;
                    if (!_firstDone)
                    {
                        lock (LockForInitializationIssue)
                        {
                            if (!_firstDone)
                            {
                                images.Read(sourceStream, settings);
                                done = true;
                            }
                        }
                    }

                    if (!done)
                    {
                        images.Read(sourceStream, settings);
                    }

                    var lastImage =
                        Math.Min(createPdfImageTaskParams.FromPage - 1 + createPdfImageTaskParams.Pages, images.Count) -
                        1;
                    for (int page = createPdfImageTaskParams.FromPage - 1; page <= lastImage; page++)
                    {
                        var image = images[page];
                        image.Format = imageFormat;

                        using (var ms = new MemoryStream())
                        {
                            image.Write(ms);
                            ms.Seek(0L, SeekOrigin.Begin);
                            await pageWriter(page + 1, ms).ConfigureAwait(false);
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName))
            {
                File.Delete(tempFileName);
            }
            return(true);
        }
        public async Task<Boolean> Run(
            String pathToFile, 
            CreatePdfImageTaskParams createPdfImageTaskParams, 
            Func<int, Stream, Task<Boolean>> pageWriter)
        {
            String tempFileName = null;
            if (Passwords.Count > 0)
            {
                tempFileName = 
                    Path.Combine(Path.GetDirectoryName(pathToFile),
                    Path.GetFileNameWithoutExtension(pathToFile) + "_decrypted.pdf");
                if (Decryptor.DecryptFile(pathToFile, tempFileName, Passwords))
                {
                    pathToFile = tempFileName;
                }
            }
            using (var sourceStream = File.OpenRead(pathToFile))
            {
                var settings = new MagickReadSettings
                {
                    Density = new PointD(createPdfImageTaskParams.Dpi, createPdfImageTaskParams.Dpi)
                };
                settings.FrameIndex = 0; // First page
                settings.FrameCount = 1; // Number of pages
                MagickFormat imageFormat = TranslateFormat(createPdfImageTaskParams.Format);

                Logger.DebugFormat("Image format is {0}", imageFormat.ToString());
                using (var images = new MagickImageCollection())
                {
                    bool done = false;
                    if (_firstDone == false)
                    {
                        lock (LockForInitializationIssue)
                        {
                            if (_firstDone == false)
                            {
                                images.Read(sourceStream, settings);
                                done = true;
//                            _firstDone = true;
                            }
                        }
                    }

                    if (!done)
                        images.Read(sourceStream, settings);

                    var lastImage =
                        Math.Min(createPdfImageTaskParams.FromPage - 1 + createPdfImageTaskParams.Pages, images.Count) -
                        1;
                    for (int page = createPdfImageTaskParams.FromPage - 1; page <= lastImage; page++)
                    {
                        var image = images[page];
                        image.Format = imageFormat;

                        using (var ms = new MemoryStream())
                        {
                            image.Write(ms);
                            ms.Seek(0L, SeekOrigin.Begin);
                            await pageWriter(page + 1, ms);
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName))
            {
                File.Delete(tempFileName);
            }
            return true;
        }
        MagickFormat TranslateFormat(CreatePdfImageTaskParams.ImageFormat format)
        {
            switch (format)
            {
                case CreatePdfImageTaskParams.ImageFormat.Png:
                    return MagickFormat.Png;

                case CreatePdfImageTaskParams.ImageFormat.Jpg:
                    return MagickFormat.Jpg;
                
                default:
                    throw new ArgumentOutOfRangeException("format");
            }
        }