Skip to content

Commit d6626c3

Browse files
snyderrachristopherdro
authored andcommitted
add ability to pass page size to android (#116)
* Update PdfConverter.java update to use letter. need to use options passed * add ability to pass page size to android * missed closing parentheses * added missing import * fixed convert to bool * forgot new for constructor
1 parent ac5ff41 commit d6626c3

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ export default class Example extends Component {
9393
| `fileName` | `string` | Random | Custom Filename excluding .pdf extension
9494
| `base64` | `boolean` | false | return base64 string of pdf file (not recommended)
9595
| `directory` | `string` |default cache directory| Directory where the file will be created (`Documents` folder in example above). Please note, on iOS `Documents` is the only custom value that is accepted.
96+
| `height` | number | 792 | Set document height (points)
97+
| `width` | number | 612 | Set document width (points)
9698

9799

98100
#### iOS Only
99101

100102
| Param | Type | Default | Note |
101103
|---|---|---|---|
102-
| `height` | number | 792 | Set document height (points)
103-
| `width` | number | 612 | Set document width (points)
104104
| `paddingLeft` | number | 10 | Outer left padding (points)
105105
| `paddingRight` | number | 10 | Outer right padding (points)
106106
| `paddingTop` | number | 10 | Outer top padding (points)

android/src/main/java/android/print/PdfConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private PrintAttributes getDefaultPrintAttrs() {
144144
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null;
145145

146146
return new PrintAttributes.Builder()
147-
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
147+
.setMediaSize(PrintAttributes.MediaSize.NA_LETTER)
148148
.setResolution(new PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_ID", 600, 600))
149149
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
150150
.build();

android/src/main/java/com/christopherdro/htmltopdf/RNHTMLtoPDFModule.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import android.os.Environment;
1616
import android.print.PdfConverter;
17+
import android.print.PrintAttributes;
1718

1819
public class RNHTMLtoPDFModule extends ReactContextBaseJavaModule {
1920

@@ -22,6 +23,8 @@ public class RNHTMLtoPDFModule extends ReactContextBaseJavaModule {
2223
private static final String DIRECTORY = "directory";
2324
private static final String BASE_64 = "base64";
2425
private static final String BASE_URL = "baseURL";
26+
private static final String HEIGHT = "height";
27+
private static final String WIDTH = "width";
2528

2629
private static final String PDF_EXTENSION = ".pdf";
2730
private static final String PDF_PREFIX = "PDF_";
@@ -76,20 +79,36 @@ public void convert(final ReadableMap options, final Promise promise) {
7679
destinationFile = getTempFile(fileName);
7780
}
7881

82+
PrintAttributes pagesize=null;
83+
if(options.hasKey(HEIGHT) && options.hasKey(WIDTH)) {
84+
pagesize=new PrintAttributes.Builder()
85+
.setMediaSize(new PrintAttributes.MediaSize("custom","CUSTOM",
86+
(int)(options.getInt(WIDTH)*1000/72.0),
87+
(int)(options.getInt(HEIGHT)*1000/72.0))
88+
)
89+
.setResolution(new PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_ID", 600, 600))
90+
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
91+
.build();
92+
}
93+
7994
convertToPDF(htmlString,
8095
destinationFile,
8196
options.hasKey(BASE_64) && options.getBoolean(BASE_64),
8297
Arguments.createMap(),
8398
promise,
84-
options.hasKey(BASE_URL) ? options.getString(BASE_URL) : null);
99+
options.hasKey(BASE_URL) ? options.getString(BASE_URL) : null,
100+
pagesize
101+
);
85102
} catch (Exception e) {
86103
promise.reject(e);
87104
}
88105
}
89106

90107
private void convertToPDF(String htmlString, File file, boolean shouldEncode, WritableMap resultMap, Promise promise,
91-
String baseURL) throws Exception {
92-
PdfConverter.getInstance().convert(mReactContext, htmlString, file, shouldEncode, resultMap, promise, baseURL);
108+
String baseURL,PrintAttributes printAttributes) throws Exception {
109+
PdfConverter pdfConverter=PdfConverter.getInstance();
110+
if(printAttributes!=null) pdfConverter.setPdfPrintAttrs(printAttributes);
111+
pdfConverter.convert(mReactContext, htmlString, file, shouldEncode, resultMap, promise, baseURL);
93112
}
94113

95114
private File getTempFile(String fileName) throws IOException {

0 commit comments

Comments
 (0)