Giter VIP home page Giter VIP logo

Comments (3)

luisfuertes avatar luisfuertes commented on June 20, 2024

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

from react-native-print.

luisfuertes avatar luisfuertes commented on June 20, 2024

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;
        }
    }
}

from react-native-print.

luisfuertes avatar luisfuertes commented on June 20, 2024

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

from react-native-print.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.