Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF printing view issue

I have tried in two ways,

1) Am creating a WebView and loading my pdf document, and my application is almost done with its part of the printing process. But in that am facing printing issue.

After printing am getting like this

Its not with full A4 sheet view.Can anyone please help,The following code i have used,

    public void createWebPagePrint(WebView webView) {
    PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
    PrintDocumentAdapter printAdapter = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        printAdapter = webView.createPrintDocumentAdapter();
        String jobName = getString(R.string.app_name) + " Document";
        PrintAttributes.Builder builder = null;
        builder = new PrintAttributes.Builder();
        builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
        PrintJob printJob = null;
        printJob = printManager.print(jobName, printAdapter, builder.build());
        if (printJob.isCompleted()) {
            Toast.makeText(getApplicationContext(), "Print Complete", Toast.LENGTH_LONG).show();
        } else if (printJob.isFailed()) {
            Toast.makeText(getApplicationContext(), "Print Failed", Toast.LENGTH_LONG).show();
        }
        builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
                .setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 1024, 720))
                .setColorMode(PrintAttributes.COLOR_MODE_COLOR).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
    }


}

Note:

https://developer.android.com/training/printing/html-docs.html

  • And some times while loading pdf its not displaying.

2) I have tried using with pdf view lib ,

 compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

But that time am getting better view compared to webview. The problem is only visible view is drawing on canvas.The print view is not clear.Its not readable.I have given the page count, So according to the page count its repeating the pages but print view is same as in first page.The following view am getting while printing. enter image description here

This is my sample code,

code

If anyone know please help me.

like image 594
Sunisha Sindhu Avatar asked Mar 06 '18 06:03

Sunisha Sindhu


1 Answers

The above procedure is very hard.Even am not getting solution for that.After that i come up with a solution and its working perfectly for me. 1) To view PDF file no need to load with webview or external pdf libraries.Just download the pdf file and view it with default pdf viewer.The below code i have used,

To download a file,

    import android.app.Activity;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FileDownloader {
    private static final int  MEGABYTE = 1024 * 1024;

    public static void downloadFile(String fileUrl, File directory, Activity activity){
        try {

            URL url = new URL(fileUrl);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            //urlConnection.setRequestMethod("GET");
            //urlConnection.setDoOutput(true);
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(directory);
            int totalSize = urlConnection.getContentLength();

            byte[] buffer = new byte[MEGABYTE];
            int bufferLength = 0;
            while((bufferLength = inputStream.read(buffer))>0 ){
                fileOutputStream.write(buffer, 0, bufferLength);
            }
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class DownloadFile extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... strings) {
        String fileUrl = strings[0];
        String fileName = strings[1];
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "Test");
        folder.mkdir();
        File pdfFile = new File(folder, fileName);
        try {
            pdfFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileDownloader.downloadFile(fileUrl, pdfFile,InventoryStockActivity.this);
        return null;
    }
}
public void download(String viewUrl) {
    new DownloadFile().execute(viewUrl, "Test.pdf");
    Log.d("Download complete", "----------");
}

To view a pdf file;

public void view() {
    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Test/" + "Test.pdf");
    Uri path = Uri.fromFile(pdfFile);
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    try {
        startActivity(pdfIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(InventoryStockActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
    }
}

In manifest,

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

And when its open default pdf viewer, there will be print menu.Just print from there.

like image 104
Sunisha Sindhu Avatar answered Nov 09 '22 01:11

Sunisha Sindhu