How to Generate PDF File from a Xml/HTML Template in iOS

Is there any way to generate PDF file from a XML/HTML template in iOs

I do this in my app using the iOS print subsystem and the UIMarkupTextPrintFormatter. The trick is to write your own custom UIPrintPageRenderer that overrides and returns correct values from paperRect and numberOfPages. You'll add your UIMarkupTextPrintFormatter(s) to your custom UIPrintPageRenderer.

Then, you'll need routines similar to this, in the context of your custom UIPrintPageRenderer:

- (CGRect) paperRect
{
if (!_generatingPdf)
return [super paperRect];

return UIGraphicsGetPDFContextBounds();
}

- (CGRect) printableRect
{
if (!_generatingPdf)
return [super printableRect];

return CGRectInset( self.paperRect, 20, 20 );
}

- (NSData*) printToPDF
{
_generatingPdf = YES;

NSMutableData *pdfData = [NSMutableData data];

UIGraphicsBeginPDFContextToData( pdfData, CGRectMake(0, 0, 792, 612), nil ); // letter-size, landscape

[self prepareForDrawingPages: NSMakeRange(0, 1)];

CGRect bounds = UIGraphicsGetPDFContextBounds();

for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();

[self drawPageAtIndex: i inRect: bounds];
}

UIGraphicsEndPDFContext();

_generatingPdf = NO;

// NSString* filename = @"/Volumes/Macintosh HD 2/test.pdf";
// [pdfData writeToFile: filename atomically: YES];

return pdfData;
}

Generating a PDF using the new printing stuff in iOS 4.2

It seems as though print formatters (including UIMarkupTextPrintFormatter) aren't actually rendered/drawn until right before printing, once the system takes over and starts the print job. (Apple's docs say drawRect: is called right before printing to provide the content for the print job.)

Someone please prove me wrong because I need to do basically the same thing :)

iOS: Drawing text on multiple pages in a PDF document

You could attempt to generate your PDF using the iOS print subsystem via UIMarkupTextPrintFormatter. I explain the technique here:

Is there any way to generate PDF file from a XML/HTML template in iOs

I haven't tried to see what would happen re. pagination, but in theory it would do the right thing.

How to use Xcode to populate PDF and HTML templates with user-defined variable data

I think it would be easier to create a HTML document but for printing, PDF is better. This is because, as far as I know, HTML is rendered differently depending on the browser but PDF will always look the same.

To create HTML you could just write plain text to a file so you could write a template before-hand, read that template into a string in memory, search for fields and insert the user's data into that string, and write a new file.

For PDF it would be more complex. I would suggest having a look at PDF-kit for high-level and Quartz for low-level PDF programming.

UIPrintPageRenderer not drawing anything with UIMarkupTextPrintFormatter

-_-! finally something!

Seems like I need to use:

[pageRenderer addPrintFormatter:html startingAtPageAtIndex:0];

instead of:

[pageRenderer drawPrintFormatter:html forPageAtIndex:0];

After I did that, it started working.

Is there a way to create reports in PDF for iOS?

Here is the official documentation for creating pdf's.



Related Topics



Leave a reply



Submit