问题 如何将Android视图转换为PDF


我创建了一个Android Invoice应用程序。生成的发票是带有嵌套视图的标准Android布局。我正在寻找一个可用于将此视图转换为pdf文档的库。

令我感到惊讶的是,我的搜索中没有直接的选项,或者我最后做的第一件事。或者也许我正在寻找的是不可能的。

请有人帮我指点一个工具,它可以帮助我从Android视图转换或生成PDF。我愿意接受免费且适度的付费选择。或者让我知道,如果我正在寻找的是不可能的。


12311
2018-04-19 13:20


起源



答案:


在您的设备屏幕上:

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

如果你有 ScrollView 作为根视图然后:

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)

这里是 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;
}

它将显示整个屏幕,包括隐藏在您的内容中 ScrollView。 现在我们有了位图屏幕,让我们将其保存为pdf(你必须下载 itextpdf-5.3.2.jar 文件并附加在你的项目..)

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();
    }
}

没有测试任何东西。以下是我使用的所有来源: 来源1源2source3


12
2018-04-19 14:36



有意思,我正在探索你的建议 - Val Okafor
凉。让我知道它是否有效...如果你能够重新创建相同的布局 WebView 那么你应该寻找一种保存的方法 html 到pdf ... - Marco Dufal
谢谢,精彩的想法,它几乎可以工作。然而,它截取了包括Actionbar在内的所有内容。它也是最终的pdf也有点模糊。再次感谢您的建议。它打开了我的眼睛,我将继续看到其他选择。再次感谢您的贡献。 - Val Okafor
所以欣赏它先生@ValOkafor - Elltz
看起来很有趣,但你不需要支付文本许可费吗? - david


答案:


在您的设备屏幕上:

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

如果你有 ScrollView 作为根视图然后:

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)

这里是 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;
}

它将显示整个屏幕,包括隐藏在您的内容中 ScrollView。 现在我们有了位图屏幕,让我们将其保存为pdf(你必须下载 itextpdf-5.3.2.jar 文件并附加在你的项目..)

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();
    }
}

没有测试任何东西。以下是我使用的所有来源: 来源1源2source3


12
2018-04-19 14:36



有意思,我正在探索你的建议 - Val Okafor
凉。让我知道它是否有效...如果你能够重新创建相同的布局 WebView 那么你应该寻找一种保存的方法 html 到pdf ... - Marco Dufal
谢谢,精彩的想法,它几乎可以工作。然而,它截取了包括Actionbar在内的所有内容。它也是最终的pdf也有点模糊。再次感谢您的建议。它打开了我的眼睛,我将继续看到其他选择。再次感谢您的贡献。 - Val Okafor
所以欣赏它先生@ValOkafor - Elltz
看起来很有趣,但你不需要支付文本许可费吗? - david


您可以使用自定义库,例如 https://github.com/HendrixString/Android-PdfMyXml。我比其他任何方法都更喜欢这个库。只是通过这些。

但还有另一种方法 - 如何将Android视图转换为PDF - 生成包含布局位图的pdf


3
2017-11-28 08:46