iclems / iOS-htmltopdf

MIT License
305 stars 79 forks source link

increase margin for first page #41

Open mrhaxic opened 7 years ago

mrhaxic commented 7 years ago

Hi

I used this class to print pdf from HTML string, I need to add header only for the first page and increase the TOP MARGIN only for the first page how could I do like this ?

Code edited as below:

- (NSData*) printToPDF
{
    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );

    [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];

    CGRect bounds = UIGraphicsGetPDFContextBounds();

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

        [self drawPageAtIndex: i inRect: bounds];
        [self drawFooterForPageAtIndex:i inRect:bounds];

        if (i == 0) {
            [self drawHeaderForPageAtIndex:i inRect:bounds];

        }
    }

    UIGraphicsEndPDFContext();

    return pdfData;
}

-(void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {

    CGPoint startPoint = CGPointMake(headerRect.origin.x, headerRect.origin.y + headerRect.size.height - 1);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);

    CGContextBeginPath(context);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextMoveToPoint(context, startPoint.x + 5, 50);
    CGContextAddLineToPoint(context, headerRect.size.width - 5, 50);
    CGContextStrokePath(context);

}