Convert Android layout to PDF file

How to convert android layout to PDF file. Is it possible?.
If possible, how to proceed to convert Android layout to PDF. Suggestions are welcome. Thanks in advance.

+4


source to share


3 answers


I have tried many ways.
Finally got an answer Using this library https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.0.6
Layout for image and put it in PDF




import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

String dirpath;
public void layoutToImage(View view) {
    // get view group using reference  
    relativeLayout = (RelativeLayout) view.findViewById(R.id.print);
    // convert view group to bitmap
    relativeLayout.setDrawingCacheEnabled(true);
    relativeLayout.buildDrawingCache();
    Bitmap bm = relativeLayout.getDrawingCache();
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public void imageToPDF() throws FileNotFoundException {
    try {
        Document document = new Document();
        dirpath = android.os.Environment.getExternalStorageDirectory().toString();
        PdfWriter.getInstance(document, new FileOutputStream(dirpath + "/NewPDF.pdf")); //  Change pdf name.
        document.open();
        Image img = Image.getInstance(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");  
        float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                - document.rightMargin() - 0) / img.getWidth()) * 100; 
        img.scalePercent(scaler);
        img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP);
        document.add(img);
        document.close();
        Toast.makeText(this, "PDF Generated successfully!..", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {

    }
}

      

+3


source


You can use a custom library like https://github.com/HendrixString/Android-PdfMyXml but there is another way which is explained here -   How to Convert Android View to PDF - which generate a PDF file containing a bitmap of your layout



+2


source


The above answer is correct, it throws an exception error on the next line.

 bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

      

Bcoz of this line of code returns zero

  Bitmap bm = relativeLayout.getDrawingCache();

      

So I did some more research about bitmap bitmap. I am using this method which converts the view to image first. Then you can use the above function i.e. imageToPDF () which works well. Below is my method.

 public void convertCertViewToImage()
 {

        scrollView.setDrawingCacheEnabled(true);
        scrollView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        scrollView.layout(0, 0, scrollView.getMeasuredWidth(), scrollView.getMeasuredHeight());
        scrollView.buildDrawingCache();
        Bitmap bm = Bitmap.createBitmap(scrollView.getDrawingCache());
        scrollView.setDrawingCacheEnabled(false); // clear drawing cache
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpg");

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File f = new File(getExternalFilesDir(null).getAbsolutePath() + File.separator + "Certificate" + File.separator + "myCertificate.jpg");

        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

} 

      

+2


source







All Articles