Skip to content

Commit

Permalink
feat(ios): add backgroundColor option
Browse files Browse the repository at this point in the history
  • Loading branch information
Canciller committed Jun 16, 2023
1 parent 3b6ec42 commit 30da57f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 3 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function App() {
const [isLoading, setIsLoading] = React.useState(false);
const [width, setWidth] = React.useState<number | undefined>(594);
const [height, setHeight] = React.useState<number | undefined>(842);
const [backgroundColor, setBackgroundColor] = React.useState('#fff');
const [backgroundColor, setBackgroundColor] = React.useState('red');
const [imageFit, setImageFit] = React.useState<ImageFit | undefined>(
'contain'
);
Expand Down Expand Up @@ -96,11 +96,13 @@ export default function App() {
placeholder="Page background color"
value={backgroundColor}
onChangeText={setBackgroundColor}
autoCapitalize="none"
/>
<TextInput
style={styles.input}
placeholder="Image fit"
defaultValue={imageFit}
autoCapitalize="none"
onChangeText={(text) => {
const f = text.toLowerCase();
switch (f) {
Expand Down
12 changes: 11 additions & 1 deletion ios/CreatePdfOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
20 changes: 14 additions & 6 deletions ios/ImagesPdf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 30da57f

Please sign in to comment.