2015-04-19 19 views
11

Ho creato un'app Android Invoice. La fattura generata è un layout standard di Android con viste nidificate. Sto cercando una libreria che posso usare per convertire questa vista in un documento pdf.Come convertire Android Visualizza in PDF

Sono sorpreso che non vi sia alcuna opzione diretta nella mia ricerca o forse ho fatto la prima cosa per ultima. O forse quello che sto cercando non è possibile.

Qualcuno potrebbe aiutarmi a indicarmi uno strumento che mi aiuti a convertire o generare un PDF da una vista Android. Sono aperto all'opzione gratuita e modesta. Oppure fammi sapere se ciò che sto cercando non è possibile.

risposta

11

Prendere uno schermo al dispositivo:

Bitmap screen; 
View v1 = MyView.getRootView(); 
v1.setDrawingCacheEnabled(true); 
screen= Bitmap.createBitmap(v1.getDrawingCache()); 
v1.setDrawingCacheEnabled(false); 

Se hai ScrollView come vista radice allora:

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null); //RelativeLayout is root view of my UI(xml) file. 
root.setDrawingCacheEnabled(true); 
Bitmap screen= getBitmapFromView(this.getWindow().findViewById(R.id.relativelayout)); // here give id of our root layout (here its my RelativeLayout's id) 

Ecco il metodo getBitmapFromView():

public static Bitmap getBitmapFromView(View view) { 
    //Define a bitmap with the same size as the view 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); 
    //Bind a canvas to it 
    Canvas canvas = new Canvas(returnedBitmap); 
    //Get the view's background 
    Drawable bgDrawable =view.getBackground(); 
    if (bgDrawable!=null) 
     //has background drawable, then draw it on the canvas 
     bgDrawable.draw(canvas); 
    else 
     //does not have background drawable, then draw white background on the canvas 
     canvas.drawColor(Color.WHITE); 
    // draw the view on the canvas 
    view.draw(canvas); 
    //return the bitmap 
    return returnedBitmap; 
} 

E ' visualizzerà l'intero schermo incluso il contenuto nascosto nel tuo ScrollView . Ora che abbiamo il nostro schermo bitmap salviamo in PDF (è necessario scaricare il file itextpdf-5.3.2.jar e allegare nel progetto ..)

private static String FILE = "mnt/sdcard/invoice.pdf"; // add permission in your manifest... 

try 
{ 
    Document document = new Document(); 

    PdfWriter.getInstance(document, new FileOutputStream(FILE)); 
    document.open(); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    screen.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] byteArray = stream.toByteArray(); 
    addImage(document,byteArray); 
    document.close(); 
} 

catch (Exception e) 
{ 
    e.printStackTrace(); 
} 

private static void addImage(Document document,byte[] byteArray) 
{ 
    try 
    { 
     image = Image.getInstance(byteArray); 
    } 
    catch (BadElementException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (MalformedURLException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    // image.scaleAbsolute(150f, 150f); 
     try 
     { 
     document.add(image); 
    } catch (DocumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

non ho ancora testato nulla. Qui ci sono tutte le fonti che ho usato: source1, source2, source3.

+0

Interessante, sto esplorando il vostro suggerimento in questo momento –

+0

Cool. Fammi sapere se funziona ... Se riesci a ricreare lo stesso layout in un 'WebView', dovresti cercare un modo per salvare' html' in pdf ... –

+2

Grazie, brillante idea, funziona quasi . Tuttavia ha preso screenshot di tutto compreso la barra delle azioni. Anche il pdf finale è un po 'sfocato. Grazie ancora per il suggerimento. Mi ha aperto gli occhi e continuerò a vedere altre alternative. Grazie ancora per il tuo grande contributo. –

5

È possibile utilizzare una libreria personalizzata come https://github.com/HendrixString/Android-PdfMyXml. Preferisco questa libreria di qualsiasi altro metodo. Basta andare attraverso questi.

, ma c'è un altro modo - Come convertire Android View to PDF - che generare un PDF che contiene bitmap del layout