<commit_before>package com.pdf.generator;

import android.print.PDFConfig;
import android.os.Build;
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.webkit.WebView;

import org.apache.cordova.LOG;

/**
 * Created by cesar on 22/01/2017.
 */

public class PDFPrinter extends PrintDocumentAdapter {

    static final String APPNAME = "PDFPrinter";

    private PrintDocumentAdapter mWrappedInstance = null;
    private WebView webView = null;
    private PrintAttributes attributes = null;
    private PDFConfig config = null;

    public PDFPrinter(WebView webView, String fileName) {
        if (Build.VERSION.SDK_INT >= 21) {
            this.mWrappedInstance = webView.createPrintDocumentAdapter(fileName);
        } else {
            this.mWrappedInstance = webView.createPrintDocumentAdapter();
        }
        config = new PDFConfig();
        this.webView = webView;
    }

    private PrintAttributes configureAttributes(PrintAttributes attrs) {

        LOG.i(APPNAME, "Adding Printer Attributes");
        return new PrintAttributes.Builder()
            .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
            .setResolution(attrs.getResolution())
            .setMinMargins(new PrintAttributes.Margins(5000,10,5000,10)).build();
    }

    @Override
    public void onStart() {
        mWrappedInstance.onStart();
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
        CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
      
        mWrappedInstance.onLayout(
            this.configureAttributes(oldAttributes),
            this.configureAttributes(newAttributes),
            cancellationSignal,
            callback,
            extras
        );
    }

    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
        WriteResultCallback callback) {
        mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback);
    }

    @Override
    public void onFinish() {
        LOG.i(APPNAME, "Cleaning pdfwriter & webView objects.");
        mWrappedInstance.onFinish();
    }<commit_msg>Changes to allow an HTML string to be rendered properly in sizes other than A4 as described in issue #30<commit_after>package com.pdf.generator;

import android.print.PDFConfig;
import android.os.Build;
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.webkit.WebView;

import org.apache.cordova.LOG;

/**
 * Created by cesar on 22/01/2017.
 */

public class PDFPrinter extends PrintDocumentAdapter {

    static final String APPNAME = "PDFPrinter";

    private PrintDocumentAdapter mWrappedInstance = null;
    private WebView webView = null;
    private PrintAttributes attributes = null;
    private PDFConfig config = null;

    public PDFPrinter(WebView webView, String fileName) {
        if (Build.VERSION.SDK_INT >= 21) {
            this.mWrappedInstance = webView.createPrintDocumentAdapter(fileName);
        } else {
            this.mWrappedInstance = webView.createPrintDocumentAdapter();
        }
        config = new PDFConfig();
        this.webView = webView;
    }

    private PrintAttributes configureAttributes(PrintAttributes attrs) {

        LOG.i(APPNAME, "Adding Printer Attributes");
		// to solve issue #60 https://github.com/cesarvr/pdf-generator/issues/60
		return attrs;
        //return new PrintAttributes.Builder()
        //    .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
        //    .setResolution(attrs.getResolution())
        //    .setMinMargins(new PrintAttributes.Margins(5000,10,5000,10)).build();
    }

    @Override
    public void onStart() {
        mWrappedInstance.onStart();
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
        CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
      
        mWrappedInstance.onLayout(
            this.configureAttributes(oldAttributes),
            this.configureAttributes(newAttributes),
            cancellationSignal,
            callback,
            extras
        );
    }

    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
        WriteResultCallback callback) {
        mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback);
    }

    @Override
    public void onFinish() {
        LOG.i(APPNAME, "Cleaning pdfwriter & webView objects.");
        mWrappedInstance.onFinish();
    }
{'fraction_non_alphanumeric': 0.08429423459244532, 'fraction_numerical': 0.010536779324055666, 'mean_word_length': 3.234848484848485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6510947', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1302, 'n_words': 300}