Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4Plus,
                    Out         = @"D:\Temp\DinkToPdf.pdf",
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        Page = "https://www.whatsmybrowser.org/"
                               //Page = "https://ywa-740-browser-check--friendly-hermann-eceb82.netlify.app/plan/week?group=93&week=2020-09-29&avoidBrowserCheck=1",
                    }
                }
            };

            var converter = new BasicConverter(new PdfTools());

            converter.Convert(doc);
        }
Exemplo n.º 2
0
        static void SingleThreadExample(string htmlContent)
        {
            var converter = new BasicConverter(new PdfTools());

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        // PagesCount = true,
                        // HtmlContent = htmlContent,
                        WebSettings ={ DefaultEncoding                   = "utf-8" },
                        // HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
                        Page = "boleto/modbol.html"
                    }
                }
            };
            var pdf    = converter.Convert(doc);
            var base64 = Convert.ToBase64String(pdf);

            Console.WriteLine(base64);
        }
Exemplo n.º 3
0
        public byte[] ConvertHtmlCodeToPdf(string html, string footerHml)
        {
            var converter = new BasicConverter(new PdfTools());

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = html,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        FooterSettings ={ Center                               = footerHml, Line = true, Spacing = 2.812},
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            return(pdf);
        }
Exemplo n.º 4
0
        public byte[] ConvertToPDF(byte[] wordBytes, string fileName)
        {
            var htmlString = ConvertToHTML(wordBytes, fileName);

            var converter = new BasicConverter(new PdfTools());

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = DinkToPdf.ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = htmlString,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            var result = converter.Convert(doc);

            return(result);
        }
        public FileResult DescargarPDF()
        {
            var convertidor = new BasicConverter(new PdfTools());
            var doc         = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.Letter,
                },

                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount  = true,
                        HtmlContent = @"" + hacerPdf(),

                        WebSettings =
                        {
                            DefaultEncoding = "utf-8"
                        },
                    }
                }
            };

            byte[] pdf = convertidor.Convert(doc);

            return(File(pdf, "application/pdf", "Solicitud.pdf"));
        }
Exemplo n.º 6
0
        private static void GeneratePdf()
        {
            var converter         = new BasicConverter(new PdfTools());
            HtmlToPdfDocument doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode      = ColorMode.Color,
                    Orientation    = Orientation.Portrait,
                    PaperSize      = PaperKind.A4,
                    UseCompression = true //defult = true
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = @"<html style='background-color:gray;'><dir style='color:orange;'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut odio viverra, molestie lectus nec, venenatis turpis.</div></html>",
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        FooterSettings ={ FontSize                             =9, Right = "Przemyslaw Bak - Page [page] of [toPage]", Line = true }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            ByteArrayToFile("dupa.pdf", pdf);
        }
Exemplo n.º 7
0
        private static void GeneratePdf()
        {
            var converter = new BasicConverter(new PdfTools());
            var doc       = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = @"<dir style='color:orange;'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut odio viverra, molestie lectus nec, venenatis turpis.</div>",
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            ByteArrayToFile("dupa.pdf", pdf);
        }
Exemplo n.º 8
0
        public static void GeneratePDF(string html, string outPath)
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4Plus,
                    Margins     = new MarginSettings()
                    {
                        Top     =         10
                    },
                    Out         = outPath,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = html,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            var converter = new BasicConverter(new PdfTools());

            converter.Convert(doc);
        }
Exemplo n.º 9
0
        public void GeneratePdfReport()
        {
            Log.Message("[PdfReportFromPipelineGenerator] generating pdf report");

            String html = GetPipelineAsHtml();

            BasicConverter converter = new BasicConverter(new PdfTools());

            HtmlToPdfDocument doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4Plus,
                    Out         = Path.Combine(OutputDirectory, "test.pdf")
                },

                Objects =
                {
                    new ObjectSettings()
                    {
                        HtmlContent    = html,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            converter.Convert(doc);
        }
Exemplo n.º 10
0
        public static byte[] Generate(List <Route> routes, int courseCount, DateTime startTime)
        {
            string HTML = GenerateHTML(routes, courseCount, startTime);

            var globalSettings = new GlobalSettings
            {
                ColorMode     = ColorMode.Color,
                Orientation   = Orientation.Portrait,
                PaperSize     = PaperKind.A4,
                DPI           = 300,
                DocumentTitle = "Schema",
                // Out = @"X:\test.pdf"
            };

            var objectSettings = new ObjectSettings
            {
                HtmlContent = HTML,
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = GetTemplatePath("document.css") },
            };

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects        = { objectSettings }
            };

            File.WriteAllText(@"X:\test.html", HTML);

            var converter = new BasicConverter(new PdfTools());

            return(converter.Convert(doc));
        }
Exemplo n.º 11
0
        private static void GeneratePdf()
        {
            var converter         = new BasicConverter(new PdfTools());
            HtmlToPdfDocument doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode      = ColorMode.Color,
                    Orientation    = Orientation.Portrait,
                    PaperSize      = PaperKind.A4,
                    UseCompression = true //defult = true
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = GetHtml(),
                        WebSettings    = { DefaultEncoding = "utf-8", EnableJavascript = true,                                            },
                        FooterSettings ={ FontSize                             =       9, Right            = "Przemyslaw Bak - Page [page] of [toPage]", Line = true}
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            ByteArrayToFile("dupa.pdf", pdf);
        }
Exemplo n.º 12
0
        public static void CreatePDFFromHtml(string htmlPage)
        {
            // https://github.com/rdvojmoc/DinkToPdf
            // https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4/64%20bit
            var converter = new BasicConverter(new PdfTools());
            var doc       = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4Plus,
                    Out         = @"test.pdf",
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = htmlPage,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
                    }
                }
            };

            converter.Convert(doc);
        }
        public IActionResult PdfTest()
        {
            var converter = new BasicConverter(new PdfTools());

            var v = Menu() as ViewResult;



            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = @"<h1>A test PDF</h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut                               odio viverra, molestie lectus nec, venenatis turpis.",
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            return(File(pdf, "application/pdf"));
        }
Exemplo n.º 14
0
        public IActionResult GetTeste()
        {
            var converter = new BasicConverter(new PdfTools());
            //var converter = new SynchronizedConverter(new PdfTools());


            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    PageOffset  = 2
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut                               odio viverra, molestie lectus nec, venenatis turpis.",
                        WebSettings    = { DefaultEncoding = "utf-8"                   },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 },
                        FooterSettings ={ Line                                 = true, Center = "Page [page] of [toPage]"},
                    }
                }
            };

            var file = converter.Convert(doc);

            return(File(file, "application/pdf"));
        }
Exemplo n.º 15
0
        public static void Main(string[] args)
        {
            //Console.ReadLine();
            var converter = new BasicConverter(new PdfTools());

            EventHandler <PhaseChangedArgs>    phaseChanged    = Converter_PhaseChanged;
            EventHandler <ProgressChangedArgs> progressChanged = Converter_ProgressChanged;
            EventHandler <FinishedArgs>        finished        = Converter_Finished;
            EventHandler <WarningArgs>         warning         = Converter_Warning;
            EventHandler <ErrorArgs>           error           = Converter_Error;

            converter.PhaseChanged    += phaseChanged;
            converter.ProgressChanged += progressChanged;
            converter.Finished        += finished;
            converter.Warning         += warning;
            converter.Error           += error;

            foreach (var file in Directory.EnumerateFiles("Input"))
            {
                var doc = new HtmlToPdfDocument
                {
                    GlobalSettings =
                    {
                        Orientation = Orientation.Portrait,
                        PaperSize   = PaperKind.A4,
                    }
                };

                doc.Objects.Add(new ObjectSettings
                {
                    WebSettings =
                    {
                        Background = true, DefaultEncoding = "utf8", LoadImages = true
                    },
                    HtmlContent = File.ReadAllText(file, Encoding.UTF8)
                });

                var pdf = converter.Convert(doc);

                if (!Directory.Exists("Files"))
                {
                    Directory.CreateDirectory("Files");
                }

                using (var stream =
                           new FileStream(
                               Path.Combine("Files", $"{Path.GetFileNameWithoutExtension(file)}.{DateTime.UtcNow.Ticks}.pdf"),
                               FileMode.Create))
                {
                    stream.Write(pdf, 0, pdf.Length);
                }
            }

            converter.PhaseChanged    -= phaseChanged;
            converter.ProgressChanged -= progressChanged;
            converter.Finished        -= finished;
            converter.Warning         -= warning;
            converter.Error           -= error;
        }
Exemplo n.º 16
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string url = req.Query["url"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            url = url ?? data?.url;

            var client = new HttpClient();
            var html   = await client.GetStringAsync(url);

            var converter = new BasicConverter(new PdfTools());

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PagesCount  = true,
                        HtmlContent = html,
                        WebSettings =
                        {
                            DefaultEncoding = "utf-8"
                        },
                        HeaderSettings =
                        {
                            Left   = "Top-Left",
                            Right  = "Top-Right",
                            Center = "Top"
                        },
                        FooterSettings =
                        {
                            Left   = "Bottom-Left",
                            Right  = "Bottom-Right",
                            Center = "Bottom"
                        }
                    }
                }
            };

            log.LogInformation("Convert begin.");

            var buffer = converter.Convert(doc);

            log.LogInformation($"Convert end. {buffer.Length}");

            return(new FileStreamResult(new MemoryStream(buffer), "application/pdf"));
        }
Exemplo n.º 17
0
        public APIGatewayProxyResponse FunctionHandler(HtmlInputModel htmlInput, ILambdaContext context)
        {
            try
            {
                var converter = new BasicConverter(new PdfTools());

                var inputAsString = htmlInput.HtmlInput;
                if (htmlInput.Is64BitEncoded)
                {
                    inputAsString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(htmlInput.HtmlInput));
                }
                var pdfDocument = new HtmlToPdfDocument
                {
                    GlobalSettings =
                    {
                        ColorMode   = ColorMode.Color,
                        Orientation = Orientation.Portrait,
                        PaperSize   = PaperKind.A4,
                    },
                    Objects =
                    {
                        new ObjectSettings()
                        {
                            PagesCount     = true,
                            HtmlContent    = inputAsString,
                            WebSettings    = { DefaultEncoding = "utf-8", LoadImages = true,                      Background = true  },
                            FooterSettings ={ FontSize                             =       9, Right      = "Page [page] of [toPage]", Line       = true, Spacing = 2.812}
                        }
                    }
                };

                var pdfBuf = converter.Convert(pdfDocument);

                var converted = Convert.ToBase64String(pdfBuf);

                var response = new APIGatewayProxyResponse
                {
                    Body            = converted,
                    IsBase64Encoded = true,
                    StatusCode      = 200
                };

                return(response);
            }
            catch (Exception e)
            {
                context.Logger.Log("Error in PDFGenerator:");
                context.Logger.Log(e.Message);

                APIGatewayProxyResponse response = new APIGatewayProxyResponse
                {
                    StatusCode = 500
                };

                return(response);
            }
        }
Exemplo n.º 18
0
        public byte[] GenerateContrtactPdf(Order order, List <Damage> damages)
        {
            var vm = new ContractViewModel();

            vm.Refresh(order, damages);
            string documentContent = _templateService.RenderTemplateAsync(
                "Templates/ContractTemplate", vm);

            var output = _pdfConverter.Convert(new HtmlToPdfDocument()
            {
                Objects =
                {
                    new ObjectSettings()
                    {
                        HtmlContent = documentContent,
                    }
                }
            });

            return(output);
        }
Exemplo n.º 19
0
        public void Test1()
        {
            var converter = new BasicConverter(new PdfTools());

            converter.Finished += (_, __) => Interlocked.Increment(ref finishedCount);
            converter.Warning  += (_, __) => Interlocked.Increment(ref warningCount);
            converter.Error    += (_, __) => Interlocked.Increment(ref errorCount);

            var doc = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PagesCount  = true,
                        HtmlContent =
                            @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices iaculis. Ut et odio viverra, molestie lectus nec, venenatis turpis. Nulla quis euismod nisl. Duis scelerisque eros nec dui facilisis, sit amet porta odio varius. Praesent vitae sollicitudin leo. Sed vitae quam in massa eleifend porta. Aliquam pulvinar orci dapibus porta laoreet. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed commodo tortor eget dolor hendrerit dapibus.
                            Vivamus lorem diam, vulputate at ultrices quis, tristique eu nunc. Sed bibendum hendrerit leo. Nulla nec risus turpis. Vivamus at tortor felis. Donec eget posuere libero. Pellentesque erat nunc, molestie eget gravida vitae, eleifend a eros. Integer in tortor sed elit aliquam vehicula eget a erat. Vivamus nisi augue, venenatis ut commodo vel, congue id neque. Curabitur convallis dictum semper. Nulla accumsan urna aliquet, mattis dolor molestie, fermentum metus. Quisque at nisi non augue tempor commodo et pretium orci.
                            Quisque blandit libero ut laoreet venenatis. Morbi sit amet quam varius, euismod dui et, volutpat felis. Sed nec ante vel est convallis placerat. Morbi mollis pretium tempor. Aliquam luctus eu justo vitae tristique. Sed in elit et elit sagittis pharetra sed vitae velit. Proin eget mi facilisis, scelerisque justo in, ornare urna. Aenean auctor ante ex, eget mattis neque pretium id. Aliquam ut risus leo. Vivamus ullamcorper et mauris in vehicula. Maecenas tristique interdum tempus. Etiam mattis lorem eget odio faucibus, in rhoncus nisi ultrices. Etiam at convallis nibh. Suspendisse tincidunt velit arcu, a volutpat nulla euismod sed.
                            Aliquam mollis placerat blandit. Morbi in nibh urna. Donec nisl enim, tristique id tincidunt sed, pharetra non mi. Morbi viverra arcu vulputate risus dignissim efficitur. Vivamus dolor eros, finibus et porttitor a, pellentesque a lectus. Integer pellentesque maximus velit sit amet sollicitudin. Nulla a elit eget augue pretium luctus quis eu metus. Aenean nec dui id nibh tempor dapibus. Pellentesque dignissim ullamcorper mauris, vitae pharetra turpis sodales sit amet. Etiam et bibendum neque.
                            Nulla gravida sit amet velit eu aliquet. Etiam sit amet elit leo. Sed nec arcu tincidunt, placerat turpis quis, laoreet nulla. Aenean neque est, fringilla non nulla in, laoreet vehicula nunc. Etiam vel nisl sit amet lectus pellentesque eleifend. Etiam sed nisi dolor. Mauris quis tincidunt ex. Aliquam porta mattis tempor. Maecenas fringilla bibendum elementum. Vestibulum quis tempus libero, vitae cursus neque. Suspendisse lectus risus, lacinia consectetur enim quis, ullamcorper porta tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                        WebSettings    = { DefaultEncoding = "utf-8"                   },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true },
                        FooterSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]" }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);
            // wipe the creation date from the document
            for (int i = 129; i <= 143; i++)
            {
                pdf[i] = 0;
            }
            File.WriteAllBytes("out.pdf", pdf);

            Assert.Equal(0, errorCount);
            Assert.Equal(0, warningCount);
            Assert.Equal(1, finishedCount);

            byte[] expected = File.ReadAllBytes("expected.pdf");
            Assert.Equal(expected, pdf);
        }
Exemplo n.º 20
0
        public static void Main(string[] args)
        {
            var converter = new BasicConverter(new PdfTools());

            converter.PhaseChanged    += Converter_PhaseChanged;
            converter.ProgressChanged += Converter_ProgressChanged;
            converter.Finished        += Converter_Finished;
            converter.Warning         += Converter_Warning;
            converter.Error           += Converter_Error;

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices iaculis. Ut et odio viverra, molestie lectus nec, venenatis turpis. Nulla quis euismod nisl. Duis scelerisque eros nec dui facilisis, sit amet porta odio varius. Praesent vitae sollicitudin leo. Sed vitae quam in massa eleifend porta. Aliquam pulvinar orci dapibus porta laoreet. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed commodo tortor eget dolor hendrerit dapibus.
                                        Vivamus lorem diam, vulputate at ultrices quis, tristique eu nunc. Sed bibendum hendrerit leo. Nulla nec risus turpis. Vivamus at tortor felis. Donec eget posuere libero. Pellentesque erat nunc, molestie eget gravida vitae, eleifend a eros. Integer in tortor sed elit aliquam vehicula eget a erat. Vivamus nisi augue, venenatis ut commodo vel, congue id neque. Curabitur convallis dictum semper. Nulla accumsan urna aliquet, mattis dolor molestie, fermentum metus. Quisque at nisi non augue tempor commodo et pretium orci.
                                        Quisque blandit libero ut laoreet venenatis. Morbi sit amet quam varius, euismod dui et, volutpat felis. Sed nec ante vel est convallis placerat. Morbi mollis pretium tempor. Aliquam luctus eu justo vitae tristique. Sed in elit et elit sagittis pharetra sed vitae velit. Proin eget mi facilisis, scelerisque justo in, ornare urna. Aenean auctor ante ex, eget mattis neque pretium id. Aliquam ut risus leo. Vivamus ullamcorper et mauris in vehicula. Maecenas tristique interdum tempus. Etiam mattis lorem eget odio faucibus, in rhoncus nisi ultrices. Etiam at convallis nibh. Suspendisse tincidunt velit arcu, a volutpat nulla euismod sed.
                                        Aliquam mollis placerat blandit. Morbi in nibh urna. Donec nisl enim, tristique id tincidunt sed, pharetra non mi. Morbi viverra arcu vulputate risus dignissim efficitur. Vivamus dolor eros, finibus et porttitor a, pellentesque a lectus. Integer pellentesque maximus velit sit amet sollicitudin. Nulla a elit eget augue pretium luctus quis eu metus. Aenean nec dui id nibh tempor dapibus. Pellentesque dignissim ullamcorper mauris, vitae pharetra turpis sodales sit amet. Etiam et bibendum neque.
                                        Nulla gravida sit amet velit eu aliquet. Etiam sit amet elit leo. Sed nec arcu tincidunt, placerat turpis quis, laoreet nulla. Aenean neque est, fringilla non nulla in, laoreet vehicula nunc. Etiam vel nisl sit amet lectus pellentesque eleifend. Etiam sed nisi dolor. Mauris quis tincidunt ex. Aliquam porta mattis tempor. Maecenas fringilla bibendum elementum. Vestibulum quis tempus libero, vitae cursus neque. Suspendisse lectus risus, lacinia consectetur enim quis, ullamcorper porta tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                        WebSettings    = { DefaultEncoding = "utf-8"                   },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true },
                        FooterSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]" }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            if (!Directory.Exists("Files"))
            {
                Directory.CreateDirectory("Files");
            }

            using (FileStream stream = new FileStream(@"Files\" + DateTime.UtcNow.Ticks.ToString() + ".pdf", FileMode.Create))
            {
                stream.Write(pdf, 0, pdf.Length);
            }

            Console.ReadKey();
        }
Exemplo n.º 21
0
        /// <summary>
        /// 导出PDF文件
        /// </summary>
        public static void ExportPDF(this HtmlToPdfDocument doc)
        {
            var converter = new BasicConverter(new PdfTools());

            byte[] pdf = converter.Convert(doc);

            if (!Directory.Exists("Files"))
            {
                Directory.CreateDirectory("Files");
            }

            using (FileStream stream = new FileStream(@"Files\" + DateTime.UtcNow.Ticks.ToString() + ".pdf", FileMode.Create))
            {
                stream.Write(pdf, 0, pdf.Length);
            }
        }
Exemplo n.º 22
0
        private static void GeneratePdf(string path, IEnumerable <Chapter> chaptersHtml, MetaInformation meta)
        {
            var joinedChapters = string.Join("\n<div class=\"pagebreak\"></div>\n", chaptersHtml.Select(c => c.Html));

            var cover = string.Empty;

            var chapterContent = Resources.pdf_template
                                 .Replace("{title}", meta.Title)
                                 .Replace("{style}", Resources.style)
                                 .Replace("{body}", joinedChapters)
                                 .Replace("{cover}", cover);

            var settings = new GlobalSettings
            {
                UseCompression = true,
                PaperSize      = PaperKind.A4,
                DocumentTitle  = meta.Title
            };

            var chapterSettings = new ObjectSettings
            {
                HtmlContent = chapterContent
            };

            var pdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings = settings,
                Objects        = { chapterSettings }
            };

            var converter = new BasicConverter(new PdfTools());

            try
            {
                var bytes = converter.Convert(pdfDocument);
                File.WriteAllBytes(path, bytes);
            }
            catch (DllNotFoundException e)
            {
                Console.Error.WriteLine("Missing assemblies. Make sure libwkhtmltox is with the tool and" +
                                        " zlib1g fontconfig libfreetype6 libx11-6 libxext6 libxrender1 are installed.");
                Console.Error.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 23
0
        static void DinkToPdf_Demo()
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4Plus,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        Page = "http://google.com/",
                    },
                    new ObjectSettings()
                    {
                        PagesCount  = true,
                        HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut                               odio viverra, molestie lectus nec, venenatis turpis.",
                        WebSettings =
                        {
                            DefaultEncoding = "utf-8"
                        },
                        HeaderSettings =
                        {
                            FontSize =                         9,
                            Right    = "Page [page] of [toPage]",
                            Line     = true,                     Spacing= 2.812
                        }
                    }
                }
            };
            var converter = new BasicConverter(new PdfTools());

            byte[] pdf  = converter.Convert(doc);
            var    file = new FileStream(Directory.GetCurrentDirectory() + $"/{DateTime.Now.ToString("MMddHHmmss")}.pdf", FileMode.CreateNew, FileAccess.ReadWrite);

            file.Write(pdf, 0, pdf.Length);
            file.Close();
        }
Exemplo n.º 24
0
        public static void ConvertToPdf(string html, string ouputPath)
        {
            var converter = new BasicConverter(new PdfTools());

            converter.ProgressChanged += ConverterOnProgressChanged;
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Margins     = new MarginSettings(15, 15, 15, 15),
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount   = true,
                        HtmlContent  = html,
                        LoadSettings = new LoadSettings()
                        {
                            JSDelay = 1000, StopSlowScript = false
                        },
                        WebSettings    = { DefaultEncoding = "utf-8", EnableIntelligentShrinking = true,                PrintMediaType = true },
                        HeaderSettings ={ FontSize                         =       9, Left                       = "Title",             Right          = "[section]", Line = false, Spacing = 5},
                        FooterSettings ={ FontSize                         =       9, Left                       = "raywenderlich.com", Right          = "[page]", Line = false, Spacing = 5},
                    }
                }
            };

            Console.WriteLine("Saving PDF to " + ouputPath);
            byte[] pdf = converter.Convert(doc);

            using (FileStream stream = new FileStream(ouputPath, FileMode.Create))
            {
                stream.Write(pdf, 0, pdf.Length);
            }

            OnProgressChangedEvent = null;
        }
Exemplo n.º 25
0
        public void GeneratePDF()
        {
            var converter = new BasicConverter(new PdfTools());

            string directory = "Protocols";

            if (!Directory.Exists(Path.GetFullPath(directory)))
            {
                Directory.CreateDirectory(Path.GetFullPath(directory));
            }

            string fileName     = "protocol_" + Guid.NewGuid() + ".pdf";
            string protocolFile = Path.Combine(directory, fileName);

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Margins     = new MarginSettings
                    {
                        Top    =                   10,
                        Bottom =         10
                    },
                    Out         = protocolFile,
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        HtmlContent    = "<!doctype html> <html><body>TEST</body></html>",
                        PagesCount     = true,
                        FooterSettings ={ Right                      = "strona [page] stron [toPage]", Line                       = true, FontSize = 7 },
                        WebSettings    = { DefaultEncoding = "utf-8",              EnableIntelligentShrinking = true }
                    }
                }
            };

            converter.Convert(doc);
        }
Exemplo n.º 26
0
        private static void GeneratePdfAsync()
        {
            string name     = "Przemysław Bąk";
            string company  = "Google Inc.";
            string position = "Senior .Net developer";

            List <Project> projects = PopulateProjects();

            List <Technology> technologies = PopulateTechs();

            var converter         = new BasicConverter(new PdfTools());
            HtmlToPdfDocument doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode      = ColorMode.Color,
                    Orientation    = Orientation.Portrait,
                    PaperSize      = PaperKind.A4,
                    UseCompression = true,
                    Margins        = new MarginSettings()
                    {
                        Top        =                    7,Left= 0, Right = 0
                    }
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        HtmlContent    = CreateHtml(name, company, position, projects, technologies),
                        PagesCount     = true,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        FooterSettings ={ FontSize                             =9, Left = "Genereted by: github.com/przemyslawbak/CV_Creator", Right = "Przemyslaw Bak - Page [page] of [toPage]", Line = true }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            ByteArrayToFile("dupa.pdf", pdf);
        }
Exemplo n.º 27
0
        public void Convert()
        {
            var pathToLibFolder = Helper.GetLibFolder();

            var option = Options.Create(new WkPdfOptions
            {
                LinuxLibPath   = Path.Combine(pathToLibFolder, "libwkhtmltox.so"),
                WindowsLibPath = Path.Combine(pathToLibFolder, "libwkhtmltox.dll"),
                OsxLibPath     = Path.Combine(pathToLibFolder, "libwkhtmltox.dylib")
            });

            var service   = new WkHtmlToXService(option);
            var converter = new BasicConverter(service);

            var version = service.GetLibraryVersion();

            Assert.AreEqual("0.12.5", version);

            var bytes = converter.Convert(new PdfConfig
            {
                GlobalPdfSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                },
                PdfSettings = new List <PdfSettings>
                {
                    new PdfSettings
                    {
                        HtmlContent = html
                    }
                }
            });

            Assert.IsNotNull(bytes);

            File.WriteAllBytes("test.pdf", bytes);
        }
        private void ConvertHTMLToPDF(string html)
        {
            Log("Converting HTML to PDF");
            Log("Ignore errors like 'Qt: Could not initialize OLE (error 80010106)'", LogLevel.Warning);
            var converter = new BasicConverter(new PdfTools());

            var output = _options.Output;

            if (output == null)
            {
                output = Path.Combine(Directory.GetCurrentDirectory(), "export.pdf");
            }

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Out         = output,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = html,
                        WebSettings    = { DefaultEncoding = "utf-8"              },
                        HeaderSettings ={ FontSize                             =9, Left = _options.HeaderLeft, Center = _options.HeaderCenter, Right = _options.HeaderRight, Line = true, Spacing = 2.812 },
                        FooterSettings ={ Left                                 = _options.FooterLeft, Center = _options.FooterCenter, Right = _options.FooterRight},
                        UseLocalLinks  = true
                    }
                }
            };

            converter.Convert(doc);
            Log($"PDF created at: {output}");
        }
        public IActionResult PdfUrl([FromBody] HtmlSource htmlSource) //string htmlContent
        {
            var converter = new BasicConverter(new PdfTools());
            //CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            //context.LoadUnmanagedLibrary(@"C:\Users\stani\Desktop\storemanagementsystemweb\StoreSystem.Web\libwkhtmltox.dll");

            //var converter = new SynchronizedConverter(new PdfTools());


            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Margins     = new MarginSettings()
                    {
                        Top     =         30
                    },
                    Out         = @"D:\test.pdf",
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount = true,
                        //HtmlContent = htmlSource.Source,
                        Page           = htmlSource.Source,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                    }
                }
            };

            converter.Convert(doc);
            return(StatusCode(200, "success"));
        }
Exemplo n.º 30
0
        /// <summary>
        ///    根据模板导出列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="dataItems"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <TemplateFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems, string htmlTemplate = null) where T : class
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("文件名必须填写!", nameof(fileName));
            }

            var exporter   = new HtmlExporter();
            var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate);

            var converter = new BasicConverter(new PdfTools());
            var doc       = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4,
                    Out         = fileName,
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PagesCount     = true,
                        HtmlContent    = htmlString,
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        HeaderSettings ={ FontSize                             =9, Right = "[page]/[toPage]", Line = true, Spacing = 2.812 },
                        Encoding       = System.Text.Encoding.UTF8
                    }
                }
            };

            converter.Convert(doc);
            var fileInfo = new TemplateFileInfo(fileName, "application/pdf");

            return(fileInfo);
        }