Skip to content

Commit

Permalink
feat: move to swift
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Oct 31, 2021
1 parent 9113a75 commit 249033a
Show file tree
Hide file tree
Showing 17 changed files with 904 additions and 652 deletions.
16 changes: 0 additions & 16 deletions Classes/TiPdfmergeModule.h

This file was deleted.

107 changes: 0 additions & 107 deletions Classes/TiPdfmergeModule.m

This file was deleted.

17 changes: 17 additions & 0 deletions Classes/TiPdftools.h
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"
104 changes: 104 additions & 0 deletions Classes/TiPdftoolsModule.swift
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This is a generated file. Do not edit or your changes will be lost
*/

@interface TiPdfmergeModuleAssets : NSObject {
@interface TiPdftoolsModuleAssets : NSObject {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* This is a generated file. Do not edit or your changes will be lost
*/
#import "TiPdfmergeModuleAssets.h"
#import "TiPdftoolsModuleAssets.h"

extern NSData* filterDataInRange(NSData* thedata, NSRange range);

@implementation TiPdfmergeModuleAssets
@implementation TiPdftoolsModuleAssets

- (NSData *)moduleAsset
{
Expand Down
24 changes: 24 additions & 0 deletions Info.plist
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>
Loading

0 comments on commit 249033a

Please sign in to comment.