I'm trying to generate PDF file from HTML template. In general this approach works fine except for one thing. When I try to use custom font AvenirLTStd-Black that is included into my bundle I get empty text in PDF but it looks fine in a webView. I can't see images from my bundle also. So I can assume that UIMarkupTextPrintFormatter doesn't see my fonts files and images. Is my assumption correct? And is there any workaround for this?
Here is part of css:
<style>
@font-face {
font-family: 'AvenirLTStd-Book';
src: url('ChampagneLimousinesItalic.ttf');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'AvenirLTStd-Heavy';
src: url('ChampagneLimousinesItalic.ttf');
font-weight: normal;
font-style: normal;
}
body, html{
margin: 0;
padding: 0;
font-family: 'AvenirLTStd-Book';
font-size: 15px;
}
And code for generating:
func exportHTMLContentToPDF(HTMLContent: String) { let printPageRenderer = CustomPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
pdfFilename = "\(AppDelegate.getAppDelegate().getDocDir())/Invoice\(invoiceNumber!).pdf"
pdfData?.write(toFile: pdfFilename, atomically: true)
print(pdfFilename)
}
I encountered a similar problem, I want to print HTML with custom fonts in iOS. Here's my solution:
Since I'm not very familiar with swift, here's some Objective-c sample code:
Load html string:
UIWindow* window = [[UIApplication sharedApplication].delegate window];
UIWebView* webView = [[UIWebView alloc] initWithFrame:window.bounds];
[window addSubview:webView];
webView.delegate = self;
webView.hidden = YES;
[webView loadHTMLString:html baseURL:baseUrl];
Get UIViewPrintFormatter and print:
#pragma UIWebViewDelegate
-(void)webViewDidFinishLoad:(UIWebView*)webView
{
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
printInfo.jobName = @"Print Demo";
UIPrintInteractionController* printVC = [UIPrintInteractionController sharedPrintController];
printVC.printInfo = printInfo;
UIViewPrintFormatter* htmlFormatter = [webView viewPrintFormatter];
printVC.printFormatter = htmlFormatter;
[printVC presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
}];
webView.delegate = nil;
[webView removeFromSuperview];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.