Exemplo n.º 1
0
        public async Task <IActionResult> Get(string url, CancellationToken cancellationToken)
        {
            using var client = new WebClient();
            var html = await client.DownloadStringTaskAsync(url);

            var pdf = await _pdfGenerator.GetAsync(html, cancellationToken);

            var pdfStream = new MemoryStream();

            pdfStream.Write(pdf, 0, pdf.Length);
            pdfStream.Position = 0;
            return(new FileStreamResult(pdfStream, "application/pdf"));
        }
Exemplo n.º 2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var html = @"<!DOCTYPE html>
                        <html>
                        <head>
                        </head>
                        <body>
                            <header>
                                <h1>This is a hardcoded test</h1>
                            </header>
                            <div>
                                <h2>456789</h2>
                            </div>
                        </body>";

                var byteArray = await _pdfGenerator.GetAsync(html, stoppingToken);

                var fileName = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid().ToString()}.pdf");

                using var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                fs.Write(byteArray, 0, byteArray.Length);

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                await Task.Delay(1000, stoppingToken);
            }
        }