2017-03-07 56 views
1

问题已经在这里问:create PDF of RecyclerView in FULL length 而我也有同样的问题,因为我还没有找到解决方案,我想用全长生成RecyclerView内容的PDF。但没有找到解决办法。从RecyclerView完整内容创建PDF?

我已经尝试了所有可用的解决方案和所有可能的方法来从RecycleView生成PDF。我已经尝试

解决方案:

https://gist.github.com/PrashamTrivedi/809d2541776c8c141d9a

Take a screenshot of RecyclerView in FULL length

Convert Listview Items into a single Bitmap Image

试过这上面提到的所有解决方案,但它们中的任何不跟我和获取工作的错误,有时宽度&身高问题或某时获得空白位ap作为输出不知道为什么。

问题:

我RecyclerView与HTML内容以及在内容的意象。

考虑将以下屏幕显示为包含内容的RecyclerView。在RecyclerView具有内容

enter image description here

相同与100+项以上图像。

RecyclerView项目布局:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:fresco="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<com.facebook.drawee.view.SimpleDraweeView 
    android:id="@+id/leftImage" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:adjustViewBounds="true" 
    android:maxHeight="350dp" 
    fresco:actualImageScaleType="fitCenter" 
    fresco:placeholderImage="@color/white" /> 

<jp.wasabeef.richeditor.RichEditor 
    android:id="@+id/editor" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/margin10dp" 
    android:layout_marginRight="@dimen/margin10dp" 
    android:layout_weight="1" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" /> 

<com.facebook.drawee.view.SimpleDraweeView 
    android:id="@+id/rightImage" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:adjustViewBounds="true" 
    android:maxHeight="350dp" 
    fresco:actualImageScaleType="fitCenter" 
    fresco:placeholderImage="@color/white" /> 
</LinearLayout> 

如何产生这种从recyclerview内容的PDF?任何帮助将不胜感激。

谢谢。

+0

生成PDF的samble代码你试图当前显示屏幕保存为图片,并追加的所有图像,使单一的PDF? –

+0

@Divyesh无法工作,因为它的回收者视图 - 整个显示屏不在屏幕上。视图被回收 –

+0

@Divyesh请正确阅读问题。 –

回答

0

这里是一个视图

//create bitmap from view and returns it 
private Bitmap getBitmapFromView(View view) { 
    ScrollView hsv = (ScrollView) findViewById(R.id.scrollViewP); 
    HorizontalScrollView horizontal = (HorizontalScrollView) findViewById(R.id.hsv); 
    int totalHeight = hsv.getChildAt(0).getHeight(); 
    int totalWidth = horizontal.getChildAt(0).getWidth(); 
    Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight,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; 
} 
private static void addImage(Document document,byte[] byteArray) 
{ 
    Image image = null; 
    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(); 
    } 
} 
public void CreatePDF() 
{ 

    File folder = new File(Environment.getExternalStorageDirectory()+File.separator+"PDF Folder"); 
    folder.mkdirs(); 

    Date date = new Date() ; 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date); 

    final File myFile = new File(folder + timeStamp + ".pdf"); 
    try { 
     OutputStream output = new FileOutputStream(myFile); 
     Document document = new Document(PageSize.A4); 
     try{ 
      PdfWriter.getInstance(document, output); 
      document.open(); 
      LinearLayout view2 = (LinearLayout)findViewById(R.id.MainLayout); 

      view2.setDrawingCacheEnabled(true); 
      Bitmap screen2= getBitmapFromView(view2); 
      ByteArrayOutputStream stream2 = new ByteArrayOutputStream(); 
      screen2.compress(Bitmap.CompressFormat.JPEG,100, stream2); 
      byte[] byteArray2 = stream2.toByteArray(); 
      addImage(document,byteArray2); 

       document.close(); 
       AlertDialog.Builder builder = new AlertDialog.Builder(PaySlip.this, R.style.AppCompatAlertDialogStyle); 
       builder.setTitle("Success") 
         .setMessage("enter code herePDF File Generated Successfully.") 
         .setIcon(android.R.drawable.ic_dialog_alert) 
         .setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) 
          { 
           Intent intent = new Intent(Intent.ACTION_VIEW); 
           intent.setDataAndType(Uri.fromFile(myFile), "application/pdf"); 
           intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
           startActivity(intent); 
          } 

         }).show(); 

      //document.add(new Paragraph(mBodyEditText.getText().toString())); 
     }catch (DocumentException e) 
     { 
      //loading.dismiss(); 
      e.printStackTrace(); 
     } 

    }catch (FileNotFoundException e) 
    { 
     // loading.dismiss(); 
     e.printStackTrace(); 
    } 


} 
+0

不工作!我想从recycler-view创建PDF。 –

+0

将携带recyclerview的视图转换为位图,然后将位图转换为pdf文件。我正在使用上面的代码,并且它的工作很完美 –

+0

我已经试过不与我合作! –