ViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // ViewController.swift
  3. // Demo
  4. //
  5. // Created by Suguru Kishimoto on 2016/02/04.
  6. //
  7. //
  8. import UIKit
  9. import PDFGenerator
  10. class ViewController: UIViewController {
  11. fileprivate var outputAsData: Bool = false
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. // Do any additional setup after loading the view, typically from a nib.
  15. }
  16. override func didReceiveMemoryWarning() {
  17. super.didReceiveMemoryWarning()
  18. // Dispose of any resources that can be recreated.
  19. }
  20. fileprivate func getImagePath(_ number: Int) -> String {
  21. return Bundle.main.path(forResource: "sample_\(number)", ofType: "jpg")!
  22. }
  23. fileprivate func getDestinationPath(_ number: Int) -> String {
  24. return NSHomeDirectory() + "/sample\(number).pdf"
  25. }
  26. @objc @IBAction fileprivate func generateSamplePDFFromViews(_ sender: AnyObject?) {
  27. let v1 = UIScrollView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  28. let v2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
  29. let v3 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
  30. v1.backgroundColor = UIColor.red
  31. v1.contentSize = CGSize(width: 100, height: 100)
  32. v2.backgroundColor = UIColor.green
  33. v3.backgroundColor = UIColor.blue
  34. do {
  35. let dst = getDestinationPath(1)
  36. if outputAsData {
  37. let data = try PDFGenerator.generated(by: [v1, v2, v3])
  38. try data.write(to: URL(fileURLWithPath: dst))
  39. } else {
  40. try PDFGenerator.generate([v1, v2, v3], to: dst)
  41. }
  42. openPDFViewer(dst)
  43. } catch (let e) {
  44. print(e)
  45. }
  46. }
  47. @objc @IBAction fileprivate func generateSamplePDFFromImages(_ sender: AnyObject?) {
  48. let dst = getDestinationPath(2)
  49. autoreleasepool {
  50. do {
  51. var images = [UIImage]()
  52. (0..<3).forEach {
  53. images.append(UIImage(contentsOfFile: getImagePath($0))!)
  54. }
  55. if outputAsData {
  56. let data = try PDFGenerator.generated(by: images)
  57. try data.write(to: URL(fileURLWithPath: dst))
  58. } else {
  59. try PDFGenerator.generate(images, to: dst, dpi: .custom(144), password: "123456")
  60. }
  61. openPDFViewer(dst)
  62. } catch (let e) {
  63. print(e)
  64. }
  65. }
  66. }
  67. @objc @IBAction fileprivate func generateSamplePDFFromImagePaths(_ sender: AnyObject?) {
  68. do {
  69. let dst = getDestinationPath(3)
  70. var imagePaths = [String]()
  71. (3..<6).forEach {
  72. imagePaths.append(getImagePath($0))
  73. }
  74. if outputAsData {
  75. let data = try PDFGenerator.generated(by: imagePaths)
  76. try data.write(to: URL(fileURLWithPath: dst))
  77. } else {
  78. try PDFGenerator.generate(imagePaths, to: dst)
  79. }
  80. openPDFViewer(dst)
  81. } catch (let e) {
  82. print(e)
  83. }
  84. }
  85. @objc @IBAction fileprivate func generateSamplePDFFromPages(_ sender: AnyObject?) {
  86. let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  87. v1.backgroundColor = UIColor.red
  88. let v2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
  89. v2.backgroundColor = UIColor.green
  90. let page1 = PDFPage.view(v1)
  91. let page2 = PDFPage.view(v2)
  92. let page3 = PDFPage.whitePage(CGSize(width: 200, height: 100))
  93. let page4 = PDFPage.image(UIImage(contentsOfFile: getImagePath(1))!)
  94. let page5 = PDFPage.imagePath(getImagePath(2))
  95. let pages = [page1, page2, page3, page4, page5]
  96. do {
  97. let dst = getDestinationPath(3)
  98. if outputAsData {
  99. let data = try PDFGenerator.generated(by: pages)
  100. try data.write(to: URL(fileURLWithPath: dst))
  101. } else {
  102. try PDFGenerator.generate(pages, to: dst)
  103. }
  104. openPDFViewer(dst)
  105. } catch (let e) {
  106. print(e)
  107. }
  108. }
  109. @objc @IBAction fileprivate func generatePDFFromStackedScrollView(_: AnyObject?) {
  110. let storyboard = UIStoryboard(name: "PDFOutput", bundle: nil)
  111. let vc = storyboard.instantiateInitialViewController()!
  112. present(vc, animated: true, completion: nil)
  113. }
  114. fileprivate func openPDFViewer(_ pdfPath: String) {
  115. let url = URL(fileURLWithPath: pdfPath)
  116. let storyboard = UIStoryboard(name: "PDFPreviewVC", bundle: nil)
  117. let vc = storyboard.instantiateInitialViewController() as! PDFPreviewVC
  118. vc.setupWithURL(url)
  119. present(vc, animated: true, completion: nil)
  120. }
  121. @objc @IBAction fileprivate func goSampleTableView(_ sender: AnyObject?) {
  122. let storyboard = UIStoryboard(name: "SampleTableViewController", bundle: nil)
  123. let vc = storyboard.instantiateInitialViewController() as! SampleTableViewController
  124. present(vc, animated: true, completion: nil)
  125. }
  126. @objc @IBAction fileprivate func goSampleWebView(_ sender: AnyObject?) {
  127. let storyboard = UIStoryboard(name: "WebViewController", bundle: nil)
  128. let vc = storyboard.instantiateInitialViewController() as! WebViewController
  129. present(vc, animated: true, completion: nil)
  130. }
  131. }