Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android): parity for WebView.createPDF() (#13657)
* feat(android): parity for WebView.createPDF() * documentation * docs * rename constants
- Loading branch information
Showing
4 changed files
with
276 additions
and
5 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
android/modules/android/src/java/android/print/PdfPrint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package android.print; | ||
|
||
import android.os.CancellationSignal; | ||
import android.os.ParcelFileDescriptor; | ||
|
||
import org.appcelerator.kroll.common.Log; | ||
import org.appcelerator.titanium.util.TiFileHelper; | ||
|
||
import java.io.File; | ||
|
||
public class PdfPrint | ||
{ | ||
private static final String TAG = PdfPrint.class.getSimpleName(); | ||
private final PrintAttributes printAttributes; | ||
File file; | ||
PageRange[] ONLY_FIRST_PAGE = new PageRange[] { new PageRange(0, 0) }; | ||
PageRange[] ALL_PAGES = new PageRange[] { PageRange.ALL_PAGES }; | ||
|
||
public PdfPrint(PrintAttributes printAttributes) | ||
{ | ||
this.printAttributes = printAttributes; | ||
} | ||
|
||
public void print(final PrintDocumentAdapter printAdapter, CallbackPrint callback, Boolean firstPageOnly) | ||
{ | ||
try { | ||
file = TiFileHelper.getInstance().getTempFile(".pdf", true); | ||
printAdapter.onLayout(printAttributes, printAttributes, null, | ||
new PrintDocumentAdapter.LayoutResultCallback() { | ||
@Override | ||
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) | ||
{ | ||
ParcelFileDescriptor outputFile = getOutputFile(); | ||
|
||
PageRange[] myRange = ALL_PAGES; | ||
if (firstPageOnly) { | ||
myRange = ONLY_FIRST_PAGE; | ||
} | ||
|
||
printAdapter.onWrite(myRange, outputFile, | ||
new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() | ||
{ | ||
@Override | ||
public void onWriteFinished(PageRange[] pages) | ||
{ | ||
super.onWriteFinished(pages); | ||
callback.success(file); | ||
} | ||
}); | ||
} | ||
}, null); | ||
} catch (Exception e) { | ||
callback.onFailure(e.getMessage()); | ||
} | ||
} | ||
|
||
private ParcelFileDescriptor getOutputFile() | ||
{ | ||
try { | ||
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); | ||
} catch (Exception e) { | ||
Log.e(TAG, "Failed to open ParcelFileDescriptor", e); | ||
} | ||
return null; | ||
} | ||
|
||
public interface CallbackPrint { | ||
void success(File file); | ||
|
||
void onFailure(String error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters