Writing text to PDF via NSString

I'm new to iOS development and this one is killing me. I did not find one complete (simple) example of how to write text in pdf on iPhone. Apple's documentation on this subject is snippets of code and is difficult to give (for me anyway), and after downloading the Quartz demo, I found that all it does is display an existing PDF file.

Simply put, at the moment I have the simplest world-based viewing application. There is one button in the view. When the button is clicked, an NSString is created that contains numbers from 1 to 15. I can write a text file to a device containing the contents of NSString, but would prefer it to be pdf.

The bottom line is that I want to create a PDF file from NSString, ultimately, for sending via email as an attachment.

If someone can point me somewhere that has a complete project that writes text in pdf, I would definitely rate it.

+4
source share
1 answer
// Create URL for PDF file NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = @"test.pdf"; NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]]; // Create PDF context CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, NULL, NULL); CGPDFContextBeginPage(pdfContext, NULL); UIGraphicsPushContext(pdfContext); // Flip coordinate system CGRect bounds = CGContextGetClipBoundingBox(pdfContext); CGContextScaleCTM(pdfContext, 1.0, -1.0); CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height); // Drawing commands [@"Hello World!" drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:72.0f]]; // Clean up UIGraphicsPopContext(); CGPDFContextEndPage(pdfContext); CGPDFContextClose(pdfContext); 
+11
source

Source: https://habr.com/ru/post/1339578/


All Articles