-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9113a75
commit 249033a
Showing
17 changed files
with
904 additions
and
652 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
// | ||
// TiPdftools.h | ||
// titanium-pdf-tools | ||
// | ||
// Created by Your Name | ||
// Copyright (c) 2021 Your Company. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
//! Project version number for TiPdftools. | ||
FOUNDATION_EXPORT double TiPdftoolsVersionNumber; | ||
|
||
//! Project version string for TiPdftools. | ||
FOUNDATION_EXPORT const unsigned char TiPdftoolsVersionString[]; | ||
|
||
#import "TiPdftoolsModuleAssets.h" |
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,104 @@ | ||
// | ||
// TiPdftoolsModule.swift | ||
// titanium-pdf-tools | ||
// | ||
// Created by Hans Knöchel | ||
// Copyright (c) 2021 Hans Knöchel. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import PDFKit | ||
import TitaniumKit | ||
|
||
let A4_WIDTH: Float = 595.2 | ||
let A4_HEIGHT: Float = 841.8 | ||
|
||
@objc(TiPdftoolsModule) | ||
class TiPdftoolsModule: TiModule { | ||
|
||
public let testProperty: String = "Hello World" | ||
|
||
func moduleGUID() -> String { | ||
return "08427de1-2112-471a-857b-357884da0f74" | ||
} | ||
|
||
override func moduleId() -> String! { | ||
return "ti.pdftools" | ||
} | ||
|
||
@objc(mergedPDF:) | ||
func mergedPDF(arguments: Array<Any>?) -> TiBlob? { | ||
guard let arguments = arguments, let paths = arguments[0] as? [String] else { return nil } | ||
|
||
let fullDocument = PDFDocument() | ||
var indexOfFullDocument = 0 | ||
|
||
for pdfPath in paths { | ||
let pdfDocument = PDFDocument(url: TiUtils.toURL(pdfPath, proxy: self)) | ||
var index = 0 | ||
var pageCount = pdfDocument?.pageCount ?? 0 | ||
|
||
while pageCount > 0 { | ||
if let pdfPage = pdfDocument?.page(at: index) { | ||
fullDocument.insert(pdfPage, at: indexOfFullDocument) | ||
index += 1 | ||
indexOfFullDocument += 1 | ||
} | ||
pageCount -= 1 | ||
} | ||
} | ||
|
||
return TiBlob(data: fullDocument.dataRepresentation(), mimetype: "application/pdf") | ||
} | ||
|
||
@objc(pdfFromImage:) | ||
func pdfFromImage(arguments: Array<Any>?) -> TiBlob? { | ||
guard let arguments = arguments?.first as? [String: Any], | ||
let image = TiUtils.image(arguments["image"], proxy: self) else { return nil } | ||
|
||
let resizeImage = arguments["resizeImage"] as? Bool ?? false | ||
|
||
// Case 1: No options | ||
guard resizeImage else { | ||
let fullDocument = PDFDocument() | ||
if let page = PDFPage(image: image) { | ||
fullDocument.insert(page, at: 0) | ||
} | ||
|
||
return TiBlob(data: fullDocument.dataRepresentation(), mimetype: "application/pdf") | ||
} | ||
|
||
// Case 2: Resized image to ft into a A4 document | ||
let padding = arguments["padding"] as? Float ?? 80 | ||
|
||
// Prepare raw data | ||
let pdfData = NSMutableData() | ||
let pdfConsumer = CGDataConsumer(data: pdfData as CFMutableData)! | ||
|
||
// Calculate the aspect ratio | ||
let imageWidth = A4_WIDTH - (padding * 2) | ||
let imageHeight = round(CGFloat(imageWidth) * (image.size.height / image.size.width)) | ||
|
||
// Calculate the bounces | ||
var mediaBox = CGRect(x: 0, | ||
y: 0, | ||
width: CGFloat(A4_WIDTH), | ||
height: CGFloat(A4_HEIGHT)); // A4 | ||
|
||
let imageBox = CGRect(x: CGFloat((A4_WIDTH / 2) - (imageWidth / 2)), | ||
y: (CGFloat(A4_HEIGHT) / 2) - (imageHeight / 2), | ||
width: CGFloat(imageWidth), | ||
height: CGFloat(imageHeight)) | ||
|
||
// Create the context to draw in | ||
let pdfContext = CGContext(consumer: pdfConsumer, mediaBox: &mediaBox, nil)! | ||
|
||
// Perform the drawing | ||
pdfContext.beginPage(mediaBox: &mediaBox) | ||
pdfContext.draw(image.cgImage!, in: imageBox) | ||
pdfContext.endPage() | ||
pdfContext.closePDF() | ||
|
||
return TiBlob(data: pdfData as Data, mimetype: "application/pdf") | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
Classes/TiPdfmergeModuleAssets.m → Classes/TiPdftoolsModuleAssets.m
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>en</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>FMWK</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
<key>NSPrincipalClass</key> | ||
<string></string> | ||
</dict> | ||
</plist> |
Oops, something went wrong.