0

I am currently implementing Android PrintService, that is able to print PDFs via thermal printers. I managed to convert PDF to bitmap using PDFRenderer and I am even able to print the document.

The thing is, the document (bitmap) is not full page width.

I am receiving the document in 297x420 resolution and I am using printer with 58mm paper.

This is how I process the document (written in C#, using Xamarin):

// Create PDF renderer
var pdfRenderer = new PdfRenderer(fileDescriptor);  

// Open page
PdfRenderer.Page page = pdfRenderer.OpenPage(index);

// Create bitmap for page
Bitmap bitmap = Bitmap.CreateBitmap(page.Width, page.Height, Bitmap.Config.Argb8888);

// Now render page into bitmap
page.Render(bitmap, null, null, PdfRenderMode.ForPrint);

And then, converting the bitmap into ESC/POS:

// Initialize result
List<byte> result = new List<byte>();

// Init ESC/POS
result.AddRange(new byte[] { 0x1B, 0x33, 0x21 });

// Init ESC/POS bmp commands (will be reapeated)
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x01, (byte)(bitmap.Width % 256), (byte)(bitmap.Height / 256) };

// Iterate height
for (int i = 0; i < (bitmap.Height / 24 + 1); i++)
{
    // Add bitmapp commands to result
    result.AddRange(escBmp);

    // Init pixel color
    int pixelColor;

    // Iterate width
    for (int j = 0; j < bitmap.Width; j++)
    {
        // Init data
        byte[] data = new byte[] { 0x00, 0x00, 0x00 };

        for (int k = 0; k < 24; k++)
        {
            if (((i * 24) + k) < bitmap.Height)
            {
                // Get pixel color
                pixelColor = bitmap.GetPixel(j, (i * 24) + k);

                // Check pixel color
                if (pixelColor != 0)
                {
                    data[k / 8] += (byte)(128 >> (k % 8));
                }
            }
        }

        // Add data to result
        result.AddRange(data);
    }

    // Add some... other stuff
    result.AddRange(new byte[] { 0x0D, 0x0A });
}

// Return data
return result.ToArray();

Current result looks like this: enter image description here

Thank you all in advance.

Filip Matys
  • 175
  • 3
  • 10

1 Answers1

2

There is no magic "scale-to-page-width" command in the ESC/POS command-set, you need to know the max width of your printer, available in the manual, and then you can:

  • Double the width and height for some image output commands -- You are using ESC *, which supports low-density, but height and width change in different ratios.
  • Render the PDF wider to begin with - match the Bitmap size to the printer page width, and not the PDF page width. The same problem is solved at PDFrenderer setting scale to screen
  • You can also simply stretch the image before you send it, if you are happy with the low quality. See: How to Resize a Bitmap in Android?

Aside, your ESC * implementation is incorrect. There are two bytes for the width- Check the ESC/POS manual for the correct usage, or read over the correct implementations in PHP or Python that I've linked in another question: ESC POS command ESC* for printing bit image on printer

mike42
  • 1,430
  • 13
  • 23
  • Scaling did the trick, thank you. Was trying it before, but was getting random characters out of the printer. Found a treshold and it now prints just fine :). – Filip Matys May 30 '18 at 13:25