Skip to content

Instantly share code, notes, and snippets.

@izzuddin91
Created July 29, 2018 03:59
Show Gist options
  • Save izzuddin91/7ad2415d698b046bd43ea2bb7235f99f to your computer and use it in GitHub Desktop.
Save izzuddin91/7ad2415d698b046bd43ea2bb7235f99f to your computer and use it in GitHub Desktop.
general code snippets
//swift 4.0
func convertpdfToImage(url: URL) -> UIImage? {
guard let document = CGPDFDocument(url as CFURL) else { return nil }
guard let page = document.page(at: 1) else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let img = renderer.image { ctx in
UIColor.white.set()
ctx.fill(pageRect)
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(page)
}
return img
}
//swift 4.0
func convertStringOfHtmlToPdf(htmlString : String, filename: String) -> String{
// 1. Create a print formatter
var returnString = ""
let html = htmlString
let fmt = UIMarkupTextPrintFormatter(markupText: html)
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAt: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
render.setValue(page, forKey: "paperRect")
render.setValue(page, forKey: "printableRect")
// 4. Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, .zero, nil)
for i in 0..<render.numberOfPages {
UIGraphicsBeginPDFPage();
render.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext();
// 5. Save PDF file
guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(filename).appendingPathExtension("pdf")
else { fatalError("Destination URL not created") }
pdfData.write(to: outputURL, atomically: true)
print("open \(outputURL.path)") // command to open the generated file
returnString = "\(outputURL.path)"
// self.globalVar.set(outputURL.path, forKey: "file_path")
return returnString
}
func convertPDFToData(pages: [PDFPage]) -> Data {
//this portion below convert back the resulting image to pdf
let pdfDocument = PDFDocument()
//iterate the list of pdf pages and insert into the PDFDocument object
for (index, element) in pages.enumerated() {
// Insert the PDF page into your document
pdfDocument.insert(element, at: index )
}
// Get the raw data of your PDF document
let data = pdfDocument.dataRepresentation()
return data!
}
let data = self.convertImageBackToPdf(pages: master_pdf)
// load the data intot the wkwebkit
self.webPreview.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: url)
// The url to save the data to
let file_path = self.saveDataToLocalFile(data: data)
globalVar.set(file_path, forKey: "file_path")
let atPoint = CGPoint(x: 400, y: 300) // to specify where we want to place the signature
let newImage = self.mergingSignatureOnPdfImage(signatureImage: signature!, pdfImage: convertpdftoimage!, atPoint: atPoint)
func mergingSignatureOnPdfImage(signatureImage : UIImage, pdfImage: UIImage, atPoint: CGPoint) -> UIImage{
UIGraphicsBeginImageContextWithOptions((pdfImage.size), false, 0.0)
//first, draw the pdf image
pdfImage.draw(in: CGRect(x: 0, y: 0, width: (pdfImage.size.width) , height: (pdfImage.size.height)))
//width and height here are adjustable according to your preference
let rect2: CGRect = CGRect(x: atPoint.x, y: atPoint.y + 140, width: 200 , height: 100 )
//Now Draw the signature into an image.
signatureImage.draw(in: rect2)
//try add the QR image here
let QR_image = Service.sharedInstance.generateQRCode(from: "https://www.apple.com") as! UIImage
let rect3: CGRect = CGRect(x: atPoint.x + 50, y: atPoint.y - 265 , width: 100 , height: 100 )
// QR_image.draw(in: rect3)
// now we get the resulting image which are signature on top of the pdf
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
// End the context now that we have the image we need
UIGraphicsEndImageContext()
return newImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment