PDFPage.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // PDFPage.swift
  3. // PDFGenerator
  4. //
  5. // Created by Suguru Kishimoto on 2016/06/21.
  6. //
  7. //
  8. import Foundation
  9. import UIKit
  10. /**
  11. PDF page model.
  12. - WhitePage: A white view (CGSize)
  13. - View: A view. (UIView)
  14. - Image: An image (UIImage)
  15. - ImagePath: ImagePath: An image path (String)
  16. - Binary: Binary data (NSData)
  17. - ImageRef: Image ref (CGImage)
  18. */
  19. public enum PDFPage {
  20. /// A white view (CGSize)
  21. case whitePage(CGSize)
  22. /// A view. (UIView)
  23. case view(UIView)
  24. /// An image (UIImage)
  25. case image(UIImage)
  26. /// ImagePath: An image path (String)
  27. case imagePath(String)
  28. /// Binary data (NSData)
  29. case binary(Data)
  30. /// Image ref (CGImage)
  31. case imageRef(CGImage)
  32. /**
  33. Convert views to PDFPage models.
  34. - parameter views: Array of `UIVIew`
  35. - returns: Array of `PDFPage`
  36. */
  37. static func pages(_ views: [UIView]) -> [PDFPage] {
  38. return views.map { .view($0) }
  39. }
  40. /**
  41. Convert images to PDFPage models.
  42. - parameter views: Array of `UIImage`
  43. - returns: Array of `PDFPage`
  44. */
  45. static func pages(_ images: [UIImage]) -> [PDFPage] {
  46. return images.map { .image($0) }
  47. }
  48. /**
  49. Convert image path to PDFPage models.
  50. - parameter views: Array of `String`(image path)
  51. - returns: Array of `PDFPage`
  52. */
  53. static func pages(_ imagePaths: [String]) -> [PDFPage] {
  54. return imagePaths.map { .imagePath($0) }
  55. }
  56. }
  57. /// PDF page size (pixel, 72dpi)
  58. public struct PDFPageSize {
  59. fileprivate init() {}
  60. /// A4
  61. public static let A4 = CGSize(width: 595.0, height: 842.0)
  62. /// B5
  63. public static let B5 = CGSize(width: 516.0, height: 729.0)
  64. }