Hallo zusammen,
ich bin ganz neu hier im Forum weil ich schon öfter bei der Problemlösung von Objective-C auf das Forum gestoßen bin und nun selber an einer Stelle nicht weiter komme.
Ich arbeite gerade an einer iPad App, mit der man einen Warenkorb befüllen kann und am Ende die Möglichkeit haben soll sich über AirPrint eine Bestellbestätigung ausdrucken zu können. Da man das ganze eventuell auch per E-Mail senden soll, gehe ich einen kleinen "Umweg" über PDF.
Ich habe mir eine eigene Subclass von UIPrintPageRenderer gebaut, in die ich im Header und Footer einzelne Objekte zeichnen kann und im Content Bereich verwende ich ein HTML Template zum generieren der Positionen des Warenkorbs.
Das HTML Template ist eine ganz einfache HTML-Table in der dann einfach Strings ersetzt werden.
Hier die Methode die das PDF generiert und dann dieses an den Drucker übergibt:
Alles anzeigen
Mit der SQL-Abfrage werden einfach die Positionen generiert und sollen als HTML dargestellt werden, das funktioniert auch ohne Probleme.
Das Problem ist das vor jeder Position ein Bild des Artikels angezeigt werden soll, die Bilder liegen alle im Library verzeichnis, ([AppDelegateX returnDocumentsPath] <-- Diese Funktion geht auf das Library Verzeichnis)
Ich habe schon probiert mit file:// , den Pfad als String oder BASE64 codiert das Image anzuzeigen und leider funktioniert nicht von allem
Zur Base 64 Generierung habe ich die QSStrings Klasse eingebunden.
Ich hoffe es jemand eine Idee.
Danke
H1990
ich bin ganz neu hier im Forum weil ich schon öfter bei der Problemlösung von Objective-C auf das Forum gestoßen bin und nun selber an einer Stelle nicht weiter komme.
Ich arbeite gerade an einer iPad App, mit der man einen Warenkorb befüllen kann und am Ende die Möglichkeit haben soll sich über AirPrint eine Bestellbestätigung ausdrucken zu können. Da man das ganze eventuell auch per E-Mail senden soll, gehe ich einen kleinen "Umweg" über PDF.
Ich habe mir eine eigene Subclass von UIPrintPageRenderer gebaut, in die ich im Header und Footer einzelne Objekte zeichnen kann und im Content Bereich verwende ich ein HTML Template zum generieren der Positionen des Warenkorbs.
Das HTML Template ist eine ganz einfache HTML-Table in der dann einfach Strings ersetzt werden.
Hier die Methode die das PDF generiert und dann dieses an den Drucker übergibt:
HTML-Quellcode
- - (IBAction)startPDFPrinting:(id)sender {
- // Controller for pdf generating
- UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- printInfo.jobName = [NSString stringWithFormat:@"Druck von Beleg: %@ - %@", AppDelegateX.currentBelegNr, [AppDelegateX returnCurrentGermanDateString]];
- printInfo.duplex = UIPrintInfoOrientationPortrait;
- printInfo.outputType = UIPrintInfoOutputGeneral;
- //> !IMPORTANT! all the printing stuff have to be done in cm not in points or pixels
- // Renderer
- PrintRenderer *printer = [[PrintRenderer alloc] initWithHeaderSize:13.3 andFooter:2.3 andSize:CGSizeMake(21.0, 29.7) andInset:0.0];
- //> now we have to add all the stuff that we want to print
- // create header objects
- [printer.headerObjects addObject:[[DrawImage alloc] initImage:[UIImage imageWithContentsOfFile:[[[[AppDelegateX returnDocumentsPath] stringByAppendingPathComponent:kMainMediaFolder] stringByAppendingPathComponent:@"icons"] stringByAppendingPathComponent:@"overlay_header.png"] ] inRect:CGRectMake(0, 0, 20.5, 7.8)]];
- [printer.headerObjects addObject:[[DrawLineText alloc] initText:@"Ich bin ein Text im \nHeader" atPoint:CGPointMake(2.2, 5.2) ofFont:[UIFont systemFontOfSize:11.0] forWidth:8.0 inColor:[UIColor blackColor] withAlignment:NSTextAlignmentLeft]];
- // create footer objects
- [printer.footerObjects addObject:[[DrawImage alloc] initImage:[UIImage imageWithContentsOfFile:[[[[AppDelegateX returnDocumentsPath] stringByAppendingPathComponent:kMainMediaFolder] stringByAppendingPathComponent:@"icons"] stringByAppendingPathComponent:@"overlay_footer.png"] ] inRect:CGRectMake(0, 0, 20.5, 2.3)]];
- // generate html content
- NSError *error;
- NSString *printPath = [[[AppDelegateX returnDocumentsPath] stringByAppendingPathComponent:kMainMediaFolder] stringByAppendingPathComponent:kMainPrintFolder];
- NSString *html = @"<!DOCTYPE html><html><body>";
- html = [html stringByAppendingString: [NSString stringWithContentsOfFile:[printPath stringByAppendingPathComponent:@"css.html"] encoding:NSUTF8StringEncoding error:&error]];
- NSString *rowHtml = [NSString stringWithContentsOfFile:[printPath stringByAppendingPathComponent:@"tpl.html"] encoding:NSUTF8StringEncoding error:&error];
- if (!error) {
- // create the rows on html base
- //float gesamtwert = 0.00;
- //int gesamtanzahl = 0;
- NSString *colums = @"artikelnr, icaauftrag";
- NSString* firmaForThisOrder = [AppDelegateX retrunFirmaForOrderID:AppDelegateX.currentBelegNr];
- NSString *sqlStatement = [NSString stringWithFormat:@"select %@ from auftragsposition where firma = '%@' AND auftragsnr = '%@' %@", colums, firmaForThisOrder, AppDelegateX.currentBelegNr, sortCondition];
- sqlite3_stmt *warenKorbLevel1;
- if(sqlite3_prepare_v2(AppDelegateX.database, [sqlStatement UTF8String], -1, &warenKorbLevel1, NULL) == SQLITE_OK) {
- int i = 1;
- //[Base64 initialize];
- while(sqlite3_step(warenKorbLevel1) == SQLITE_ROW) {
- NSString *artikelNr = [AppDelegate checkNullString:(char *)sqlite3_column_text(warenKorbLevel1, 0)];
- int artikelNrAsInteger = [artikelNr intValue];
- int icaNummer = sqlite3_column_int(warenKorbLevel1, 1);
- NSString *filledICA = [NSString stringWithFormat:@"%05d", icaNummer];
- UIImage *img = [AppDelegateX returnContentImageByName:[NSString stringWithFormat:@"produktbilder/th/%@.png", artikelNr]];
- NSString *imgStr = [QSStrings encodeBase64WithData:UIImagePNGRepresentation(img)];
- NSString *icaArtNr = [NSString stringWithFormat:@"%@.%07d.%@", [filledICA substringToIndex:3], artikelNrAsInteger, [filledICA substringFromIndex:3]];
- // Replace HTML
- NSString *row = rowHtml;
- row = [row stringByReplacingOccurrencesOfString:@"{POS}" withString:[NSString stringWithFormat:@"%03d", i]]; // POS
- row = [row stringByReplacingOccurrencesOfString:@"{IMG}" withString:[NSString stringWithFormat:@"<img src='data:image/png;base64,%@' />", imgStr]]; // IMG
- row = [row stringByReplacingOccurrencesOfString:@"{ARTICLE}" withString:icaArtNr]; // ARTICLE
- html = [html stringByAppendingString:row];
- i++;
- }
- }
- else {[AppDelegateX logDatabaseError:_cmd];}sqlite3_finalize(warenKorbLevel1);
- }
- else {
- NSLog(@"Error Loading Print Templates: %@", error);
- }
- html = [html stringByAppendingString:@"</body></html>"];
- // set the print formatter
- UIMarkupTextPrintFormatter *viewFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
- viewFormatter.startPage = 0;
- viewFormatter.contentInsets = UIEdgeInsetsMake(0, 0, 0, 0);
- [printer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
- // Create a PDF
- NSString *fileName = [NSString stringWithFormat:@"beleg_%@_%@.pdf", AppDelegateX.currentBelegNr, [AppDelegateX returnCurrentGermanDateString]];
- NSString *pdfPath = [[[[AppDelegateX returnDocumentsPath] stringByAppendingPathComponent:kMainMediaFolder] stringByAppendingPathComponent:kMainBelegeFolder] stringByAppendingPathComponent:fileName];
- UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
- for (NSInteger i=0; i < [printer numberOfPages]; i++) {
- UIGraphicsBeginPDFPage();
- CGRect bounds = UIGraphicsGetPDFContextBounds();
- [printer drawPageAtIndex:i inRect:bounds];
- }
- UIGraphicsEndPDFContext();
- NSLog(@"PDF created");
- Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
- if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
- {
- NSURL *fileURL = [NSURL fileURLWithPath:pdfPath]; // Document file URL
- UIPrintInteractionController* printInteraction = [printInteractionController sharedPrintController];
- if ([printInteractionController canPrintURL:fileURL] == YES)
- {
- UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];
- printInfo.duplex = UIPrintInfoDuplexLongEdge;
- printInfo.outputType = UIPrintInfoOutputGeneral;
- printInfo.jobName = pdfPath;
- printInteraction.printInfo = printInfo;
- printInteraction.printingItem = fileURL;
- printInteraction.showsPageRange = YES;
- [printInteraction presentFromRect:[sender frame] inView:[sender superview] animated:YES completionHandler:
- ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
- {
- if ((completed == NO) && (error != nil)) {
- UIAlertView* printAlert = [[UIAlertView alloc] initWithTitle:@"Drucken" message:[NSString stringWithFormat:@"%s %@", __FUNCTION__, error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
- [printAlert show];
- }
- else if ( completed ) {
- UIAlertView* printAlert = [[UIAlertView alloc] initWithTitle:@"Drucken" message:@"Der Druckauftrag wurde an den Drucker gesendet!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
- [printAlert show];
- NSString *update = [NSString stringWithFormat:@"UPDATE auftragskopf set druck = 'J' where auftragsnr = '%@'", AppDelegateX.currentBelegNr];
- [AppDelegateX performDatabaseQueryByString:update];
- }
- else if (error) {
- UIAlertView* printAlert = [[UIAlertView alloc] initWithTitle:@"Drucken" message:[NSString stringWithFormat:@"%s %@", __FUNCTION__, error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
- [printAlert show];
- }
- }
- ];
- }
- }
- }
Mit der SQL-Abfrage werden einfach die Positionen generiert und sollen als HTML dargestellt werden, das funktioniert auch ohne Probleme.
Das Problem ist das vor jeder Position ein Bild des Artikels angezeigt werden soll, die Bilder liegen alle im Library verzeichnis, ([AppDelegateX returnDocumentsPath] <-- Diese Funktion geht auf das Library Verzeichnis)
Ich habe schon probiert mit file:// , den Pfad als String oder BASE64 codiert das Image anzuzeigen und leider funktioniert nicht von allem
Zur Base 64 Generierung habe ich die QSStrings Klasse eingebunden.
Ich hoffe es jemand eine Idee.
Danke
H1990