matthew
matthew

Reputation: 343

iOS - how to generate PDF in landscape

I am having a hard time determining how to properly print in landscape. Here is a custom print page renderer I use as a subclass of UIPrintPageRenderer:

  @implementation CustomPrintPageRenderer

- (instancetype)init{
  self = [super init];
  if (self) {
    self.A4PageWidth = 595.2;
    self.A4PageHeight = 841.8;
    CGRect pageFrame = CGRectMake(0.0, 0.0, self.A4PageWidth, self.A4PageHeight);
    [self setValue:[NSValue valueWithCGRect:pageFrame] forKey:@"paperRect"];
    [self setValue:[NSValue valueWithCGRect:CGRectInset(pageFrame, 1.0, 1.0)] forKey:@"printableRect"];
  }
  return self;
}

@end

And here is the code that takes HTML, generates the PDF, and returns the filepath (this is a 1024 pixel wide form that must be this width for a customer):

`-(NSString *) exportHTMLContentToPDF {
  CustomPrintPageRenderer *cppr = [[CustomPrintPageRenderer alloc]init];
  UIMarkupTextPrintFormatter *printFormatter = [[UIMarkupTextPrintFormatter alloc]initWithMarkupText:self.finalHTML];
  [cppr addPrintFormatter:printFormatter startingAtPageAtIndex:0];
  NSData * pdfData = [self drawPDF:cppr];
  NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.ts.timesheetID];
  NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  [pdfData writeToFile:filePath atomically:YES];
  return filePath;
}
-(NSData *)drawPDF:(CustomPrintPageRenderer *)cppr {
  NSMutableData *data = [[NSMutableData alloc]init];
  UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
  UIGraphicsBeginPDFPage();
  [cppr drawPageAtIndex:0 inRect:UIGraphicsGetPDFContextBounds()];
  UIGraphicsEndPDFContext();
  return data;
}`

Currently this prints the page fine in Portrait, but since its wide it forces it to fit the portrait width and shrinks content. If I swap the A4 height/width sizes it no longer shrinks the content, but cuts off about 20% (eye balling here) of the content from the right.

EDIT: This will only work for single page document. Looking at how I can transform the PDF context before drawing it.

So maybe I am thinking of this wrong. Adjusting the width/height to make it layout like landscape does nothing. It still prints portrait. So I need to determine how to keep the portrait sizes, but rotate the entire HTML page to the left 90 degrees before generating the PDF data... Will update later if this works or not. Otherwise any other advice would be greatly appreciated!

Upvotes: 1

Views: 1273

Answers (1)

Mrugesh Tank
Mrugesh Tank

Reputation: 3560

I had same problem, after searching nothing found. So I just looked into my own code for some modification. Here is the code which met my expectation.

just replace
UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
with following
UIGraphicsBeginPDFContextToData(data, CGRectMake(0, 0, A4PageHeight, A4PageWidth), nil);
while generating pdf from context.

also change CustomPrintPageRenderer's paperRect as like below.
CGRect pageFrame = CGRectMake(0.0, 0.0, self.A4PageHeight, self.A4PageWidth);

Upvotes: 4

Related Questions