Android Tutorial [SAMPLE CODE] Print from your Android app

Discussion in 'Tutorials & Examples' started by NJDude, Aug 27, 2012.

  1. NJDude

    NJDude Expert Licensed User

    This sample will show you how to print from your smartphone or tablet using Google Cloud Print (login required).

    Requirements:

    1 - Google GMail account.

    2 - The web browser you use MUST allow popups.

    3 - Internet connection.

    4 - A Printer connected to Google Cloud Print (Follow these instructions to add a Cloud-Ready Printer or a Classic Printer.

    5 - Google Chrome installed on your desktop/laptop to do the initial printer setup.

    After you have setup your printer, just compile and run the attached project.

    This sample project is a simplified version that will allow you to print directly from your app to any printer added to the Cloud Print service, the sample includes some of the types of documents that can be printed, for other types (such as Google Docs) read THIS document (bottom of the page).

    Limitations:

    * If you use Chrome this sample will work on devices running ICS or higher, also, some devices with custom ROMs might fail too.


    I tried to use a WebView but it didn't work, that's why I'm using the browser.

    If someone finds a way to do it using a WebView or a better way, please share the knowledge.


    Thanks

    UPDATE (Nov 26, 2013): There's a better alternative to this method using a library, see THIS thread.
     

    Attached Files:

    Last edited: Dec 12, 2013
  2. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Great job!
    Really usefull!

    Tomas
     
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Another great sample :)

    EDIT: You can also save stuff to your GDrive with this!
     
    Last edited: Aug 28, 2012
  4. NJDude

    NJDude Expert Licensed User

    Yes, if no printer is setup by default you have the GDrive and FedEx printer.
     
  5. bherrington0421

    bherrington0421 Member Licensed User

    This is such an awesome tool. Thanks for putting this together. I'm getting this error - has anyone else encountered this? I can't find PrintJob.html anywhere in your project files and the path it's looking for it definitely isn't right. I'm running this in a 4.1 Emulator.
     

    Attached Files:

  6. NJDude

    NJDude Expert Licensed User

    You get that error because the HTML is to be written on the SDCard the emulator doesn have one, unless you configure it.
     
  7. wleupold

    wleupold Member Licensed User

    Print RTF file to cloud

    Hi everybody,
    I try to use the cloud print example. In my app I generate an rtf file and want this print out with a cloud printer.
    In the example I can use different mimetypes, but there is no doc/file type.
    How can I send an rtf file to the cloud an print it?

    The example works fine if the rtf file is located on my server:
    SendToCloud("url", "http://www.mydomain.com/testfile.rtf")

    But what are the Parameters to use for the testfile.rft if located on my device?

    Thanks for any help

    Werner:BangHead:
     
  8. Geezer

    Geezer Active Member Licensed User

    Hi NJ,

    I'm trying to print using your example as is.

    I get a web page saying can't find the file, and nothing prints.

    The printer is online and shows OK as a google cloud printer.

    Any ideas ?
    nofile.png
     
  9. Geezer

    Geezer Active Member Licensed User

    No Ideas ?
     
  10. Mike Olmsted

    Mike Olmsted Member Licensed User

    I have Google drive working perfect, my printer is fine, I can print any file. But, I want to print from within my program. Where do I insert my file name to Cloud Print Sample?
    Code:
    Sub PrintURLButton_Click   
          
    'SendToCloud("url", "http://www.google.com/landing/cloudprint/testpage.pdf")
            SendToCloud("url""file:///" & File.DirDefaultExternal & "/bookmarks.html")       
    End Sub
    Thanks in advance, Mike
     
  11. NJDude

    NJDude Expert Licensed User

    Try:
    Code:
    SendToCloud("text""file:///" & File.DirDefaultExternal & "/bookmarks.html")
    Note that the line above will print the HTML content, not the rendering.
     
    Mike Olmsted likes this.
  12. Mike Olmsted

    Mike Olmsted Member Licensed User

    That prints the line same as print text button.
    I tried url, and text for type
    and .pdf, .txt, .html, and .csv as files.
    Most common error was cannot fetch the url or would print nothing.
    I can print csv to file then convert to pdf , and then print it with no problem. Issue is automating the process.
     
  13. NJDude

    NJDude Expert Licensed User

    Read the first post, I've added a modified version and I named it: "CloudPrinting_BETA", this new code will require that you enter the MIME type, let me know if that works.

    Check the code, I added a couple of notes there.

    Thanks
     
    Last edited: Oct 1, 2013
    Mike Olmsted likes this.
  14. NJDude

    NJDude Expert Licensed User

    New alternative to use CloudPrint, please see first post.
     
  15. Mike Olmsted

    Mike Olmsted Member Licensed User

    Here is my implementation of NJDudes' Cloud printing sample.

    1. Print any table
    2. Print any query from your database
    3. Open drive to see the reports.
    4. Backup your data file by sending it to someone.

    Wish list.....name the reports before sending so you don't have a dozen reports
    named B4A Report.pdf.
     

    Attached Files:

    Mashiane likes this.
  16. GMan

    GMan Well-Known Member Licensed User

    Very useful - 'cause the paperless bureau is more far away then before 20 years :cool:
     
  17. luke2012

    luke2012 Well-Known Member Licensed User


    @NJDude outstanding tutorial! There is a way to start a print job progammatically without the browser?
    For example :

    User tap on a button and the print job is created.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice