christopherdro / react-native-print

Print documents using React Native
MIT License
325 stars 135 forks source link

Is there any way to set paper size as IS0 5 default #169

Open Venkat1P opened 2 years ago

Venkat1P commented 2 years ago

Any support for setting a default paper size before print.

Venkat1P commented 2 years ago

Hi @christopherdro do you have any suggestions for passing paper size as option. Please suggest this is getting critical for my project..

Chhinvanchhai commented 2 years ago

i research it for long, but still don't find solution. I want to print for label size.(IOS)

reendev commented 2 years ago

Modify android/src/java/com/christopherdro/RNPrint/PNPrintModule.java

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

    final String paperSize = options.hasKey("paperSize") ? options.getString("paperSize") : null;

    ...

    @Override
    public void onPageFinished(WebView view, String url) {

            ....

            // Pass in the ViewView's document adapter.
            PrintAttributes.MediaSize mediaSize;
            if (paperSize != null) {
                switch(paperSize) {
                    case "receipt": mediaSize = PrintAttributes.MediaSize.ISO_A7;break;
                    default: mediaSize = isLandscape?PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE:
                        PrintAttributes.MediaSize.UNKNOWN_PORTRAIT;
                }
            }
            else {
                mediaSize = isLandscape?PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE:
                    PrintAttributes.MediaSize.UNKNOWN_PORTRAIT;
            }
            PrintAttributes printAttributes = new PrintAttributes.Builder()
                .setMediaSize(mediaSize)
                .build();
            printManager.print(jobName, adapter, printAttributes);

            ...

    }
}

Usage in react native

RNPrint.print({
   html:html,
   paperSize:'receipt'
});
hotaryuzaki commented 1 year ago

Modify android/src/java/com/christopherdro/RNPrint/PNPrintModule.java

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

    final String paperSize = options.hasKey("paperSize") ? options.getString("paperSize") : null;

    ...

    @Override
    public void onPageFinished(WebView view, String url) {

            ....

            // Pass in the ViewView's document adapter.
            PrintAttributes.MediaSize mediaSize;
            if (paperSize != null) {
                switch(paperSize) {
                    case "receipt": mediaSize = PrintAttributes.MediaSize.ISO_A7;break;
                    default: mediaSize = isLandscape?PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE:
                        PrintAttributes.MediaSize.UNKNOWN_PORTRAIT;
                }
            }
            else {
                mediaSize = isLandscape?PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE:
                    PrintAttributes.MediaSize.UNKNOWN_PORTRAIT;
            }
            PrintAttributes printAttributes = new PrintAttributes.Builder()
                .setMediaSize(mediaSize)
                .build();
            printManager.print(jobName, adapter, printAttributes);

            ...

    }
}

Usage in react native

RNPrint.print({
   html:html,
   paperSize:'receipt'
});

i think you should make a pull request, so everyone can use this. it is an important feature.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

iskandarzhilmi commented 4 months ago

This issue shouldn't be closed