plessl / wkpdf

A command line HTML to PDF converter for Mac OS X
http://plessl.github.com/wkpdf
MIT License
184 stars 24 forks source link

Support for arbitrary paper sizes and margins #6

Closed plessl closed 13 years ago

plessl commented 15 years ago

Feature request suggested by Tobias Rodäbel

Index: CommandlineParser.m
===================================================================
--- CommandlineParser.m (revision 34)
+++ CommandlineParser.m (working copy)
@@ -82,7 +82,7 @@
  //_output = @"/dev/stdout";
  _paperSize = [NSPrintInfo sizeForPaperName:@"A4"];
  _paginate = YES;
-  _margin = [NSDecimalNumber notANumber];
+  _margin = 0;
  _stylesheetMedia = nil;
  _printBackground = NO;
  _paperOrientation = NSPortraitOrientation;
@@ -418,10 +418,19 @@
- (NSSize)parsePaperSize:(char *)arg
{
  NSString * paperName = [NSString stringWithUTF8String:arg];
-  NSSize size = [NSPrintInfo sizeForPaperName:paperName];
-  if ((size.width == 0.0) && (size.height == 0.0)){
-    fprintf(stderr,"%s is not a valid paper format!\n",arg);
-    [Helper terminateWithErrorcode:1 andMessage:@"invalid paper format"];
+  NSSize size;
+  if ([paperName matchesPattern:@"[0-9]*x[0-9]*"]){
+    float point = 2.834645669;
+    NSArray *dim = [paperName componentsSeparatedByString:@"x"];
+    float width = [[dim objectAtIndex:0] floatValue];
+    float height = [[dim objectAtIndex:1] floatValue];
+    size = NSMakeSize(width*point, height*point);
+  } else {
+    size = [NSPrintInfo sizeForPaperName:paperName];
+    if ((size.width == 0.0) && (size.height == 0.0)){
+      fprintf(stderr,"%s is not a valid paper format!\n",arg);
+      [Helper terminateWithErrorcode:1 andMessage:@"invalid paper format"];
+    }
  }
  return size;
}