I am trying to print a PDF to a thermal printer via Bluetooth connection in Xamarin Forms. Print text only works correctly. But when I send the PDF it prints with strange symbols. I don't know much about thermal printers, but I would appreciate any help.
This is the method that creates the PDF
public byte[] Invoice()
{
using (MemoryStream stream = new MemoryStream())
{
//Creamos instancia de los objetos necesarios para construir la estructura del PDF
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4);
document.SetMargins(2, 2, 2, 2);
PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
PdfFont fontTitulo = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);
//Datos de la empresa, factura y del cliente a mostrar
Paragraph cabecera = new Paragraph("FACTURA").SetHorizontalAlignment(HorizontalAlignment.CENTER).SetTextAlignment(TextAlignment.CENTER).SetFontSize(11);
Paragraph saltoLinea = new Paragraph(new Text("\n"));
string dataQR = "395410026|0|0|0|0|0";
byte[] qr = generaQR(dataQR);
ImageData imageDataQR = ImageDataFactory.Create(qr);
iText.Layout.Element.Image imageQR = new iText.Layout.Element.Image(imageDataQR).ScaleToFit(80f, 80f);
document.Add(cabecera);
document.Add(saltoLinea);
document.Add(imageQR);
document.Close();
byte[] pdfBytes = stream.ToArray();
return pdfBytes;
}
}
And this is the way I send to the thermal printer
public async Task Print(string deviceName, byte[] pdfBytes)
{
using (BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter)
{
BluetoothDevice device = (from bd in bluetoothAdapter?.BondedDevices
where bd?.Name == deviceName
select bd).FirstOrDefault();
try
{
using (BluetoothSocket bluetoothSocket = device?.
CreateRfcommSocketToServiceRecord(
UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")))
{
bluetoothSocket?.Connect();
bluetoothSocket?.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);
bluetoothSocket.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
}