Question

In Swift, I'm trying to generate a pdf version of a collectionView, and save that PDF...

In Swift, I'm trying to generate a pdf version of a collectionView, and save that PDF to the temporary directory. However, I'm not sure if I'm doing it right, or actually even saving anything. Here's my code:

        func createPdfFromView(aView: UICollectionView, saveToDocumentsWithFileName fileName: String)
        {
            let pdfData = NSMutableData()
       
       
            UIGraphicsBeginPDFContextToData(pdfData, gridView.bounds, nil)
            UIGraphicsBeginPDFPage()
       
            guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
       
            aView.layer.render(in: pdfContext)
            UIGraphicsEndPDFContext()
       
            let fileName = sampleClass.sampleStruct.samplePDF.rawValue
            let template = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
       

                debugPrint(fileName)
                pdfData.write(toFile: String (describing: template), atomically: true)
        }
   how can I do this?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer for the given Question:

See the below code it may helps to you for the given problem statement

  1. func createPDFFromView(aView: NSView, saveToDocumentWithFileName fileName: String) {  
  2.         let pdfData = aView.dataWithPDFInsideRect(aView.frame)  
  3.         if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {  
  4.             let documentsFileName = documentDirectories + "/" + fileName  
  5.             debugPrint(documentsFileName)  
  6.             pdfData.writeToFile(documentsFileName, atomically: true)  
  7.         }  
  8. }
Add a comment
Know the answer?
Add Answer to:
In Swift, I'm trying to generate a pdf version of a collectionView, and save that PDF...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Barcelona Restaurant Group Summary: Barcelona Restaurant Group is always trying to attract and retain only those...

    Barcelona Restaurant Group Summary: Barcelona Restaurant Group is always trying to attract and retain only those employees who reinforce its service-oriented culture and provide top quality customer service. The manager being interviewed in the video is constantly recruiting and hiring new employees and letting low performers and poor fits go. He also tries to provide job candidates with a realistic description of the company's expectations to ensure that they know what they would be getting into if they took a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT