Skip to content

Commit

Permalink
feat: convertendo imagem do danfe nativo em pdf byte
Browse files Browse the repository at this point in the history
  • Loading branch information
robertorp committed Nov 2, 2023
1 parent eb28cc7 commit 0234d89
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
6 changes: 3 additions & 3 deletions NFe.AppTeste/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,9 +1767,9 @@ private void BtnCupom_Click(object sender, RoutedEventArgs e)



//impr.Imprimir(salvarArquivoPdfEm: fileDialog.FileName.Replace(".pdf", "") + ".pdf");
var bytes = impr.GerarImagem();
var base64 = Convert.ToBase64String(bytes);
impr.Imprimir(salvarArquivoPdfEm: fileDialog.FileName.Replace(".pdf", "") + ".pdf");
//var bytes = impr.PdfBytes();
//var base64 = Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
Expand Down
51 changes: 50 additions & 1 deletion NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
using NFe.Utils;
using NFe.Utils.InformacoesSuplementares;
using NFe.Utils.NFe;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using NFeZeus = NFe.Classes.NFe;

namespace NFe.Danfe.Nativo.NFCe
Expand Down Expand Up @@ -165,6 +167,48 @@ public byte[] GerarImagem()
}
}

public Func<Stream> ConverterBytesParaFuncStream(byte[] bytes)
{
Func<Stream> funcStream = () =>
{
MemoryStream stream = new MemoryStream(bytes);
// Certifique-se de que a posição do stream esteja no início.
stream.Position = 0;
return stream;
};

return funcStream;
}

public byte[] ConverterImagemParaPdfBytes(byte[] imagemBytes)
{
using (MemoryStream stream = new MemoryStream())
{
using (PdfDocument pdf = new PdfDocument())
{
PdfPage page = pdf.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

XImage image = XImage.FromStream(ConverterBytesParaFuncStream(imagemBytes));

page.Width = image.PointWidth;
page.Height = image.PointHeight;


gfx.DrawImage(image, 0, 0);

pdf.Save(stream);
}

return stream.ToArray();
}
}

public byte[] PdfBytes()
{
return ConverterImagemParaPdfBytes(GerarImagem());
}

public void GerarJPEG(string filename)
{
GerarImagem(filename, ImageFormat.Jpeg);
Expand Down Expand Up @@ -262,7 +306,12 @@ private void GerarNfCe(Graphics graphics)
#region preencher itens
foreach (det detalhe in det)
{
AdicionarTexto codigo = new AdicionarTexto(g, detalhe.prod.cProd, 7);
var codigoXml = detalhe.prod.cProd;

if (detalhe.prod.cProd.Length > 7)
codigoXml = detalhe.prod.cProd.Remove(7);

AdicionarTexto codigo = new AdicionarTexto(g, codigoXml, 7);
codigo.Desenhar(x, _y);

AdicionarTexto nome = new AdicionarTexto(g, detalhe.prod.xProd, 7);
Expand Down
1 change: 1 addition & 0 deletions NFe.Danfe.Nativo/NFe.Danfe.Nativo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<None Include="Fontes\UbuntuCondensed-Regular.ttf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PdfSharpCore" Version="1.3.62" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
Expand Down

0 comments on commit 0234d89

Please sign in to comment.