Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print image from url #62

Closed
luisfuertes opened this issue Nov 20, 2018 · 3 comments
Closed

Print image from url #62

luisfuertes opened this issue Nov 20, 2018 · 3 comments

Comments

@luisfuertes
Copy link

Im trying to print a image from url but app close and have error:

java.lang.RuntimeException: Cannot print a malformed PDF file
  android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:1115)
  at android.os.Handler.dispatchMessage(Handler.java:106)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:7002)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)

Only can print pdfs?

@luisfuertes
Copy link
Author

Im trying:

      await RNPrint.print({
        html: `<img src=${imageUrl} style="width:400px;height:600px;">`
      });

i already tried diferents width/height but image always have big margins

@luisfuertes
Copy link
Author

luisfuertes commented Nov 20, 2018

I did this in RNPrintModule.java and it seems that works with image urls:

package com.christopherdro.RNPrint;

import android.content.Context;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.print.PrintManager;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.UiThreadUtil;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import android.support.v4.print.PrintHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.net.HttpURLConnection;

/**
 * NativeModule that allows JS to open emails sending apps chooser.
 */
public class RNPrintModule extends ReactContextBaseJavaModule {

    ReactApplicationContext reactContext;
    final String jobName = "Document";


    public RNPrintModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "RNPrint";
    }

    WebView mWebView;

    @ReactMethod
    public void print(final ReadableMap options, final Promise promise) {

        final String html = options.hasKey("html") ? options.getString("html") : null;
        final String filePath = options.hasKey("filePath") ? options.getString("filePath") : null;
        final boolean isLandscape = options.hasKey("isLandscape") ? options.getBoolean("isLandscape") : false;

        if ((html == null && filePath == null) || (html != null && filePath != null)) {
            promise.reject(getName(), "Must provide either `html` or `filePath`.  Both are either missing or passed together");
            return;
        }

    
        try {
            PrintHelper photoPrinter = new PrintHelper(getCurrentActivity());
            photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);


            Bitmap bitmap = getBitmapFromURL(filePath);
            photoPrinter.printBitmap("samsung note 9", bitmap);
            promise.resolve(jobName);

        } catch (Exception e) {
            promise.reject(getName(), e);
        }
        
    }

    private static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }
}

@luisfuertes
Copy link
Author

For print local images only need update Bitmap with path like this:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.droids);

https://developer.android.com/training/printing/photos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant