diff --git a/example/src/App.tsx b/example/src/App.tsx index 6e59508..126ed74 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -15,7 +15,7 @@ export default function App() { const [isLoading, setIsLoading] = React.useState(false); const [width, setWidth] = React.useState(594); const [height, setHeight] = React.useState(842); - const [backgroundColor, setBackgroundColor] = React.useState('#fff'); + const [backgroundColor, setBackgroundColor] = React.useState('red'); const [imageFit, setImageFit] = React.useState( 'contain' ); @@ -96,11 +96,13 @@ export default function App() { placeholder="Page background color" value={backgroundColor} onChangeText={setBackgroundColor} + autoCapitalize="none" /> { const f = text.toLowerCase(); switch (f) { diff --git a/ios/CreatePdfOptions.swift b/ios/CreatePdfOptions.swift index bae0696..e5ee5db 100644 --- a/ios/CreatePdfOptions.swift +++ b/ios/CreatePdfOptions.swift @@ -18,10 +18,20 @@ struct Page: Decodable { let imageFit: ImageFit? let width: Double? let height: Double? + let backgroundColor: Int? } -struct CreatePdfOptions: Decodable { +class CreatePdfOptions: Decodable { let outputDirectory: String let outputFilename: String let pages: [Page] + + init(_ options: NSDictionary) throws { + let jsonData = try JSONSerialization.data(withJSONObject: options, options: []) + let pdfCreateOptions = try JSONDecoder().decode(CreatePdfOptions.self, from: jsonData) + + self.outputDirectory = pdfCreateOptions.outputDirectory + self.outputFilename = pdfCreateOptions.outputFilename + self.pages = pdfCreateOptions.pages + } } diff --git a/ios/ImagesPdf.swift b/ios/ImagesPdf.swift index 9cb2796..07c04cd 100644 --- a/ios/ImagesPdf.swift +++ b/ios/ImagesPdf.swift @@ -10,7 +10,7 @@ class ImagesPdf: NSObject { @objc func createPdf(_ options: NSDictionary, resolver resolve:RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) { do { - let createPdfOptions = try parseOptions(options: options) + let createPdfOptions = try CreatePdfOptions(options) let outputDirectory = createPdfOptions.outputDirectory let outputFilename = createPdfOptions.outputFilename @@ -78,6 +78,13 @@ class ImagesPdf: NSObject { let pageBounds = CGRect(x: 0, y: 0, width: width, height: height) context.beginPage(withBounds: pageBounds, pageInfo: [:]) + + if let backgroudColorInt = page.backgroundColor { + let backgroundColor = createUIColor(from: backgroudColorInt).cgColor + context.cgContext.setFillColor(backgroundColor) + context.cgContext.fill(pageBounds) + } + var scaledImage: UIImage? if width != image.size.width || height != image.size.height { let fit = page.imageFit @@ -163,11 +170,12 @@ class ImagesPdf: NSObject { return docsDir } - func parseOptions(options: NSDictionary) throws -> CreatePdfOptions { - let jsonData = try JSONSerialization.data(withJSONObject: options, options: []) - - let pdfCreateOptions = try JSONDecoder().decode(CreatePdfOptions.self, from: jsonData) + func createUIColor(from color: Int) -> UIColor { + let red = CGFloat((color >> 16) & 0xFF) / 255.0 + let green = CGFloat((color >> 8) & 0xFF) / 255.0 + let blue = CGFloat(color & 0xFF) / 255.0 + let alpha = CGFloat((color >> 24) & 0xFF) / 255.0 - return pdfCreateOptions + return UIColor(red: red, green: green, blue: blue, alpha: alpha) } }