Generate and print a PDF with specific size in Android -
i'm working in android application, , want generate , print pdf. i'm having trouble. need generate pdf
80mm of width
, , height
may vary.
i'm trying this:
public class pdfgenerator implements runnable { private context ctx; private view view; private intent mshareintent; private outputstream os; public pdfgenerator(context ctx, view view) { this.ctx = ctx; this.view = view; makeandsharepdf(); } public void makeandsharepdf() { new thread(this).start(); } @targetapi(build.version_codes.kitkat) public void run() { printattributes printattrs = new printattributes.builder(). setcolormode(printattributes.color_mode_monochrome). setmediasize(printattributes.mediasize.iso_c7). setresolution(new printattributes.resolution("zooey", ctx.print_service, 500, 500)). setminmargins(printattributes.margins.no_margins). build(); pdfdocument document = new printedpdfdocument(ctx, printattrs); pdfdocument.pageinfo pageinfo = new pdfdocument.pageinfo.builder(view.getwidth(), view.getheight(), 1).create(); pdfdocument.page page = document.startpage(pageinfo); view.draw(page.getcanvas()); document.finishpage(page); try { file pdfdirpath = new file(ctx.getfilesdir(), "pdfs"); pdfdirpath.mkdirs(); file file = new file(pdfdirpath, "danfe.pdf"); uri contenturi = fileprovider.geturiforfile(ctx, "br.com.rdscodes.nfcelular", file); os = new fileoutputstream(file); document.writeto(os); document.close(); os.close(); sharedocument(contenturi); } catch (ioexception e) { throw new runtimeexception("error generating file", e); } } private void sharedocument(uri uri) { mshareintent = new intent(); mshareintent.setaction(intent.action_send); mshareintent.settype("application/pdf"); mshareintent.putextra(intent.extra_subject, "nfc-e"); mshareintent.putextra(intent.extra_stream, uri); ctx.startactivity(mshareintent); } }
this code generates pdf
, letter size
.
i saw in android developers media size documentation "iso_c7" media size size want, can change "printattrs" , nothing changes on final result of pdf.
are there better ways generate , print pdf
? how can generate pdf
80mm of width
?
this part of app made old employee.
thanks.
just don't break head on it, did, check out bug report: https://code.google.com/p/android/issues/detail?id=180405
considering bug report, i'd it's problem android printing interface , can't set defaults using printattributes, @ least not until android n.
if find/found workaround, please let me know :)
Comments
Post a Comment