I am currently working on generating a PDF file from an HTML template. Everything seems to be going smoothly with this approach, except for one issue I've encountered. When attempting to use the custom font AvenirLTStd-Black that is included in my bundle, the text appears empty in the PDF, though it displays correctly in a webView. Additionally, images from my bundle are not appearing either. It seems like UIMarkupTextPrintFormatter is unable to recognize my font files and images. Is my assumption correct? And is there a workaround for this?
Below is a snippet of the CSS being used:
@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 here is the code snippet for the generation process:
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)
}
Original tutorial link here