Ejemplo n.º 1
0
        private void GeneratePDF2()
        {
            var viewer = new RdlViewer
            {
                SourceFile = new Uri(Path.Combine(Application.StartupPath, "FE2PDF.rdl"))
            };

            foreach (var h in _data)
            {
                var header = new List <Header> {
                    h
                };
                var details = h.Items;

                viewer.Report.DataSets["Header"].SetData(header);
                viewer.Report.DataSets["Details"].SetData(details);

                viewer.Parameters  = string.Empty;
                viewer.Parameters += $@"&bg_image={Path.Combine(Application.StartupPath, $"fc_{h.CondicionIVA.ToLower()}.jpg")}";

                if (ConfigInfo.InvoiceCodeType != InvoiceCodeType.None)
                {
                    if (ConfigInfo.InvoiceCodeType == InvoiceCodeType.QR)
                    {
                        var qr = h.CondicionIVA.Equals("a", StringComparison.OrdinalIgnoreCase) || h.CondicionIVA.Equals("b", StringComparison.OrdinalIgnoreCase)
                            ? GenerateQR(h)
                            : string.Empty;

                        if (!string.IsNullOrEmpty(qr))
                        {
                            viewer.Parameters += $@"&qrcode={qr}";
                        }
                    }
                    else
                    {
                        var barcode = !string.IsNullOrEmpty(h.CodigoBarra)
                            ? Int2of5.GenerateBarCode(h.CodigoBarra, 1000, 100, 2).ToBase64()
                            : string.Empty;

                        if (!string.IsNullOrEmpty(barcode))
                        {
                            viewer.Parameters += $@"&barcode={barcode}";
                        }
                    }
                }

                viewer.Rebuild();

                var pdfName    = $@"{h.TipoComprobante}{h.CondicionIVA}{h.CentroEmisor}{h.NumeroComprobante}.pdf";
                var outputFile = Path.Combine(txtOutputFolder.Text, pdfName);

                if (File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }

                viewer.SaveAs(outputFile, OutputPresentationType.PDF);

                barProgress.PerformStep();
                lblStatus.Text = $@"Archivo PDF {barProgress.Value}/{barProgress.Maximum}";

                Application.DoEvents();
            }
        }
Ejemplo n.º 2
0
        private void Preview_Load(object sender, EventArgs e)
        {
            var data = new List <Header>();

            data.Add(new Header
            {
                Barrio            = "Barrio",
                CUILCUIT          = "20-32676067-7",
                CentroEmisor      = "Test",
                CodigoBarra       = "0123456789",
                CodigoPostal      = "1417",
                CondicionIVA      = "A",
                CondicionPago     = "Efectivo",
                Detalle1          = "Det 1",
                Detalle2          = "Det 2",
                Domicilio         = "Allende 2879",
                Email             = "*****@*****.**",
                FechaEmision      = "01/01/2018",
                FechaVencimiento  = "01/08/2018",
                Importe           = "1000",
                Localidad         = "CABA",
                MontoGravado      = "1000",
                MontoIVA          = "1000",
                MontoNoGravado    = "1000",
                NombreComprobante = "FC",
                NumeroCAE         = "ABC123456",
                NumeroComprobante = "123456",
                RefPagoMisCuentas = "123456",
                RefRedLink        = "123456",
                Subtotal          = "1000",
                TipoComprobante   = "A",
                Items             = new List <Detail>
                {
                    new Detail
                    {
                        Detalle        = "Detalle 1",
                        MontoFacturado = "1000"
                    },
                    new Detail
                    {
                        Detalle        = "Detalle 2",
                        MontoFacturado = "1000"
                    }
                }
            });

            rdlViewer1.SourceFile = new Uri(Path.Combine(Application.StartupPath, "FE2PDF.rdl"));

            foreach (var h in data)
            {
                var header = new List <Header> {
                    h
                };
                var details = h.Items;

                rdlViewer1.Report.DataSets["Header"].SetData(header);
                rdlViewer1.Report.DataSets["Details"].SetData(details);

                var barcode = !string.IsNullOrEmpty(h.CodigoBarra)
                    ? Int2of5.GenerateBarCode(h.CodigoBarra, 1000, 100, 10).ToBase64()
                    : string.Empty;

                rdlViewer1.Parameters += $@"&bg_image={Path.Combine(Application.StartupPath, $"fc_{h.CondicionIVA.ToLower()}.jpg")}";
                rdlViewer1.Parameters += $@"&barcode={barcode}";

                rdlViewer1.Rebuild();

                //var pdfName    = $@"{h.TipoComprobante}{h.CondicionIVA}{h.CentroEmisor}{h.NumeroComprobante}.pdf";
                //var outputFile = Path.Combine($@"C:\Projects\Repositories\RDM\FE2PDF\Paths\Processed", pdfName);

                //rdlViewer1.SaveAs(outputFile, OutputPresentationType.PDF);
            }
        }
Ejemplo n.º 3
0
        private void GeneratePDF()
        {
            try
            {
                var threads = new List <Thread>();

                var template = File.ReadAllText(Path.Combine(Application.StartupPath, @"template.html"));

                var start = template.IndexOf(@"<table class=""details"">", 0, StringComparison.OrdinalIgnoreCase) +
                            @"<table class=""details"">".Length;
                var end            = template.IndexOf(@"</table>", start, StringComparison.OrdinalIgnoreCase);
                var templateDetail = template.Substring(start, end - start);

                lblStatus.Text      = @"Generando templates";
                barProgress.Style   = ProgressBarStyle.Continuous;
                barProgress.Maximum = _data.Count;

                foreach (var header in _data)
                {
                    var html   = template;
                    var bgFile =
                        $"file:///{Path.Combine(Application.StartupPath, $"fc_{header.CondicionIVA.ToLower()}.jpg")}"
                        .Replace("\\", "/");

                    html = html.Replace("{{BackgroundFilePath}}", bgFile);
                    html = html.Replace("{{ShowSubtotales}}",
                                        header.CondicionIVA.Equals("b", StringComparison.OrdinalIgnoreCase)
                                            ? "hidden"
                                            : "visible");

                    header.CodigoBarra = "977123456700";

                    if (!string.IsNullOrEmpty(header.CodigoBarra))
                    {
                        var barcode = Int2of5.GenerateBarCode(header.CodigoBarra, 1000, 100, 10).ToBase64();
                        html = html.Replace("{{Barcode}}", $"data:image/png;base64, {barcode}");
                        html = html.Replace("{{show-barcode}}", "");
                    }
                    else
                    {
                        html = html.Replace("{{show-barcode}}", "display: none !important; visibility: hidden;");
                    }

                    var properties = typeof(Header).GetProperties();

                    html = properties.Aggregate(
                        html,
                        (current, property) =>
                        current.Replace($"{{{{{property.Name}}}}}", property.GetValue(header, null).ToString()));

                    var htmlDetail = string.Empty;

                    foreach (var detail in header.Items)
                    {
                        htmlDetail += templateDetail;

                        properties = typeof(Detail).GetProperties();

                        htmlDetail =
                            properties.Aggregate(
                                htmlDetail,
                                (current, property) =>
                                current.Replace($"{{{{{property.Name}}}}}",
                                                property.GetValue(detail, null).ToString()));
                    }

                    html = html.Replace(templateDetail, htmlDetail);

                    var pdfName =
                        $@"{header.TipoComprobante}{header.CondicionIVA}{header.CentroEmisor}{header.NumeroComprobante}.pdf";
                    var outputFile = Path.Combine(txtOutputFolder.Text, pdfName);

                    var p = new object[] { html, outputFile };

                    threads.Add(new Thread(delegate() { ConvertToPDF(p); }));

                    Application.DoEvents();

                    barProgress.PerformStep();
                    lblStatus.Text = $@"Template {barProgress.Value}/{barProgress.Maximum}";
                }

                var batchSize    = ConfigInfo.BatchSize;
                var currentBatch = 0;

                barProgress.Value = barProgress.Minimum;

                while (true)
                {
                    threads.Where(t => t.ThreadState == ThreadState.Unstarted).Take(batchSize).ToList()
                    .ForEach(t => t.Start());

                    currentBatch++;

                    while (threads.Any(t => t.IsAlive))
                    {
                        Application.DoEvents();
                    }

                    if (currentBatch * batchSize > threads.Count + batchSize)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }