2017-03-07 148 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

我已經試過不與我合作! –