<commit_before>package at.modalog.cordova.plugin.html2pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

import android.R.bool;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;



import android.print.PrintDocumentAdapter.LayoutResultCallback;
import android.print.PrintAttributes.MediaSize;
//import android.print.PrintDocumentInfo;
//import android.print.PrintDocumentInfo.Builder;
import android.print.PrintDocumentAdapter.WriteResultCallback;
import android.os.CancellationSignal;
import android.os.Bundle;
import android.print.PageRange;
import android.os.ParcelFileDescriptor;
import android.print.pdf.PrintedPdfDocument;
import android.util.SparseIntArray;
import android.graphics.pdf.PdfDocument.Page;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;

import android.print.PrintAttributes.Resolution;
import android.graphics.pdf.PdfDocument;
import android.graphics.pdf.PdfDocument.PageInfo;





import android.printservice.PrintJob;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.util.Log;

@TargetApi(19)
public class Html2pdf extends CordovaPlugin
{
    private static final String LOG_TAG = "Html2Pdf";
    private CallbackContext callbackContext;
	
    // change your path on the sdcard here
    private String publicTmpDir = ".at.modalog.cordova.plugin.html2pdf"; // prepending a dot "." would make it hidden
    private String tmpPdfName = "print.pdf";
	
    // set to true to see the webview (useful for debugging)
    private final boolean showWebViewForDebugging = true;
    
    PrintedPdfDocument mPdfDocument;
    WebView page;
    int totalPages = 1;
	
    /**
    * Constructor.
    */
    public Html2pdf() {
	
    }

    @Override
    public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
		try
		{
			if( action.equals("create") )
			{
				if( showWebViewForDebugging )
				{
					Log.v(LOG_TAG,"java create pdf from html called");
					Log.v(LOG_TAG, "File: " + args.getString(1));
					// Log.v(LOG_TAG, "Html: " + args.getString(0));
					Log.v(LOG_TAG, "Html start:" + args.getString(0).substring(0, 30));
					Log.v(LOG_TAG, "Html end:" + args.getString(0).substring(args.getString(0).length() - 30));
				}
				
				if( args.getString(1) != null && args.getString(1) != "null" )
					this.tmpPdfName = args.getString(1);  
				
				final Html2pdf self = this;
				final String content = args.optString(0, "<html></html>");
		        	this.callbackContext = callbackContext;
				
			        cordova.getActivity().runOnUiThread( new Runnable() {
			            public void run()
				    {
			            	if( Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT ) // Android 4.4
			            	{
			            		/*
				            	 * None-Kitkat pdf creation (Android < 4.4)
				            	 * it will be a image based pdf
				            	 */
			            		
				                self.loadContentIntoWebView(content);
			            	}
			            	else
			            	{
				            	/*
				            	 * Kitkat pdf creation by using the android print framework (Android >= 4.4)
				            	 */
			            		
						// Create a WebView object specifically for printing
						page = new WebView(cordova.getActivity());
						page.getSettings().setJavaScriptEnabled(false);
						page.setDrawingCacheEnabled(true);
					        // Auto-scale the content to the webview's width.
						page.getSettings().setLoadWithOverviewMode(true);
						page.getSettings().setUseWideViewPort(true);
						page.setInitialScale(0);
					        // Disable android text auto fit behaviour
						page.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
			            		if( showWebViewForDebugging )
			            	        {
			            			page.setVisibility(View.VISIBLE);
			            	        } 
			            	        else {
			            	        	page.setVisibility(View.INVISIBLE);
			            	        }
			            		
						// self.cordova.getActivity().addContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
			            		page.setWebViewClient( new WebViewClient()
						{
							public boolean shouldOverrideUrlLoading(WebView view, String url) {
								return false;
							}
		
							@Override
							public void onPageFinished(WebView webView, String url)
							{
							        /*PrintAttributes pdfPrintAttrs = new PrintAttributes.Builder().
							                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
							                setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
							                setResolution(new Resolution("zooey", Context.PRINT_SERVICE, 300, 300)).
							                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
							                build();
							        PdfDocument document = new PrintedPdfDocument(self.cordova.getActivity(),pdfPrintAttrs);*/
							        PdfDocument documento = new PdfDocument();
							        
							        PageInfo pageInfo = new PageInfo.Builder(595,842, 1).create();
							        Page pagina = documento.startPage(pageInfo);
							        pagina.getCanvas().setDensity(200);
							        //View content = (View) webView;
							        webView.draw(pagina.getCanvas());
							        documento.finishPage(pagina);
							        
							        try{
							             File root = Environment.getExternalStorageDirectory();          
								     File file = new File(root,"webview.pdf");
								     FileOutputStream out = new FileOutputStream(file);
								     doc.writeTo(out);
								     out.close();
								     doc.close();
							        } catch(Exception e){
								     throw new RuntimeException("Error generating file", e);
							        } finally {
								     //documento.close();
								     //documento = null;
							        }
								
						                // send success result to cordova
						                PluginResult result = new PluginResult(PluginResult.Status.OK);
						                result.setKeepCallback(false); 
				                    		self.callbackContext.sendPluginResult(result);
							}<commit_msg>Update Html2pdf.java<commit_after>package at.modalog.cordova.plugin.html2pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

import android.R.bool;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;



import android.print.PrintDocumentAdapter.LayoutResultCallback;
import android.print.PrintAttributes.MediaSize;
//import android.print.PrintDocumentInfo;
//import android.print.PrintDocumentInfo.Builder;
import android.print.PrintDocumentAdapter.WriteResultCallback;
import android.os.CancellationSignal;
import android.os.Bundle;
import android.print.PageRange;
import android.os.ParcelFileDescriptor;
import android.print.pdf.PrintedPdfDocument;
import android.util.SparseIntArray;
import android.graphics.pdf.PdfDocument.Page;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;

import android.print.PrintAttributes.Resolution;
import android.graphics.pdf.PdfDocument;
import android.graphics.pdf.PdfDocument.PageInfo;





import android.printservice.PrintJob;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.util.Log;

@TargetApi(19)
public class Html2pdf extends CordovaPlugin
{
    private static final String LOG_TAG = "Html2Pdf";
    private CallbackContext callbackContext;
	
    // change your path on the sdcard here
    private String publicTmpDir = ".at.modalog.cordova.plugin.html2pdf"; // prepending a dot "." would make it hidden
    private String tmpPdfName = "print.pdf";
	
    // set to true to see the webview (useful for debugging)
    private final boolean showWebViewForDebugging = true;
    
    PrintedPdfDocument mPdfDocument;
    WebView page;
    int totalPages = 1;
	
    /**
    * Constructor.
    */
    public Html2pdf() {
	
    }

    @Override
    public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
		try
		{
			if( action.equals("create") )
			{
				if( showWebViewForDebugging )
				{
					Log.v(LOG_TAG,"java create pdf from html called");
					Log.v(LOG_TAG, "File: " + args.getString(1));
					// Log.v(LOG_TAG, "Html: " + args.getString(0));
					Log.v(LOG_TAG, "Html start:" + args.getString(0).substring(0, 30));
					Log.v(LOG_TAG, "Html end:" + args.getString(0).substring(args.getString(0).length() - 30));
				}
				
				if( args.getString(1) != null && args.getString(1) != "null" )
					this.tmpPdfName = args.getString(1);  
				
				final Html2pdf self = this;
				final String content = args.optString(0, "<html></html>");
		        	this.callbackContext = callbackContext;
				
			        cordova.getActivity().runOnUiThread( new Runnable() {
			            public void run()
				    {
			            	if( Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT ) // Android 4.4
			            	{
			            		/*
				            	 * None-Kitkat pdf creation (Android < 4.4)
				            	 * it will be a image based pdf
				            	 */
			            		
				                self.loadContentIntoWebView(content);
			            	}
			            	else
			            	{
				            	/*
				            	 * Kitkat pdf creation by using the android print framework (Android >= 4.4)
				            	 */
			            		
						// Create a WebView object specifically for printing
						page = new WebView(cordova.getActivity());
						page.getSettings().setJavaScriptEnabled(false);
						page.setDrawingCacheEnabled(true);
					        // Auto-scale the content to the webview's width.
						page.getSettings().setLoadWithOverviewMode(true);
						page.getSettings().setUseWideViewPort(true);
						page.setInitialScale(0);
					        // Disable android text auto fit behaviour
						page.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
			            		if( showWebViewForDebugging )
			            	        {
			            			page.setVisibility(View.VISIBLE);
			            	        } 
			            	        else {
			            	        	page.setVisibility(View.INVISIBLE);
			            	        }
			            		
						// self.cordova.getActivity().addContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
			            		page.setWebViewClient( new WebViewClient()
						{
							public boolean shouldOverrideUrlLoading(WebView view, String url) {
								return false;
							}
		
							@Override
							public void onPageFinished(WebView webView, String url)
							{
							        /*PrintAttributes pdfPrintAttrs = new PrintAttributes.Builder().
							                setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
							                setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
							                setResolution(new Resolution("zooey", Context.PRINT_SERVICE, 300, 300)).
							                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
							                build();
							        PdfDocument document = new PrintedPdfDocument(self.cordova.getActivity(),pdfPrintAttrs);*/
							        PdfDocument documento = new PdfDocument();
							        
							        PageInfo pageInfo = new PageInfo.Builder(595,842, 1).create();
							        Page pagina = documento.startPage(pageInfo);
							        pagina.getCanvas().setDensity(200);
							        //View content = (View) webView;
							        webView.draw(pagina.getCanvas());
							        documento.finishPage(pagina);
							        
							        try{
							             File root = Environment.getExternalStorageDirectory();          
								     File file = new File(root,"webview.pdf");
								     FileOutputStream out = new FileOutputStream(file);
								     documento.writeTo(out);
								     out.close();
								     documento.close();
							        } catch(Exception e){
								     throw new RuntimeException("Error generating file", e);
							        } finally {
								     //documento.close();
								     //documento = null;
							        }
								
						                // send success result to cordova
						                PluginResult result = new PluginResult(PluginResult.Status.OK);
						                result.setKeepCallback(false); 
				                    		self.callbackContext.sendPluginResult(result);
							}
{'fraction_non_alphanumeric': 0.09549160994676191, 'fraction_numerical': 0.006267268683873576, 'mean_word_length': 2.418567150426169, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '1650616', 'n_tokens_mistral': 5073, 'n_tokens_neox': 3955, 'n_words': 807}