Android Question Convert pdf to bitmap??

maxh2815

Member
I'm writing text and numbers to a basic pdf using a PdfDocument object. I want to convert this to a bitmap so I can send it to my Bluetooth POS printer (through StarDust's library). I tried to use the pdf's canvas and write it to a file:
B4X:
Dim pdf As PdfDocument
pdf.Initialize
pdf.StartPage(595, 842) 'A4 size
pdf.Canvas.DrawText("Hello", 100, 100, Typeface.DEFAULT_BOLD, 35 / GetDeviceLayoutValues.Scale, Colors.Black, "LEFT")
pdf.Canvas.DrawText("Testing: 123", 100, 140, Typeface.DEFAULT_BOLD, 15 / GetDeviceLayoutValues.Scale, Colors.Black, "LEFT")
pdf.FinishPage

Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "testing.png", False)
pdf.Canvas.Bitmap.WriteToStream(out, 100, "PNG")
out.Close
Log("Wrote bitmap to file")

out = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "testing.pdf", False)
pdf.WriteToStream(out)
out.Close
pdf.Close
Log("Wrote pdf to file")

The pdf works perfectly, and looks like this:
1618378671235.png

This is a cut of the top portion of it. However, when I open the .png file that was supposedly the written bitmap, it just opens to a blank, completely white page with a very small black square in the middle. Am I saving it incorrectly perhaps? Additionally, is there a better way to send a pdf to the POS printer?

Thanks.
 

maxh2815

Member
Why not just save your text as a bitmap
I am not too sure what you mean. How would you go about doing this?


Its for an invoice + receipting system - eventually the bitmap will be much more complex and have an actual layout instead of just a list of text. So far drawing to a pdf is the easiest way I've found to achieve this - I was originally thinking of putting a canvas on a panel but with a pdf you can easily specify a size. I also want to save the pdf to the device as well as printing it so doing it this way has the benefit of only having to draw the graphics and text once.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
EDITED: To clean up too many typos, I was in a hurry.

I've created multiple bespoke invoice, stock control, JIT, Android powered 2D barcode solutions and obviously EPOS printer apps and solutions for bars, restaurants, shops, warehouses etc, trust me when I say that your idea whilst sounds good WILL in fact most probably be a disaster in real life when converting a PDF file to a bitmap file then printing that bitmap to an EPOS printer.

I strongly suggest that you have two code modules (or two codes sections in the one code module), one for creating and printing PDF files, and the other for printing to an EPOS printer. You will be storing the data in a database so retrieving the items/info and printing them to either printer will in fact server you better.

Have you actually tried to imagining what it would look like, either a A4 or a LETTER PDF file squashed onto the layout of an EPOS printer, not unless I'm missing something obvious, it will not look good. If you were to create the PDF specifically for printing on an EPOS printer layout, then the PDF itself would would have to have HUGE letters and less words per line, and in turn that itself would NOT look good to give to people/customers, that is if you are thinking about releasing the app to the public.


Enjoy...
 
Last edited:
Upvote 0

maxh2815

Member
Ok thanks. Sounds like a good way of doing it. Regardless though, it would be interesting to get an answer to my question - why the bitmap of the pdf is not coming out as expected. Any thoughts on this?
 
Upvote 0
Top