Hello, I have a question about PDF data creation on iOS 17.
My app creates PDF data from HTML for printing, and it is not possible to print out the PDF data to a particular printer when the app runs on iOS 17, despite that it works fine on iOS 16 and earlier.
The cause of the printing problem is unclear, however, I found that the contents of PDF data are different between iOS 17 and iOS 16 in the following 4 points:
PDF version
iOS 17: Ver. 1.4
iOS 16: Ver. 1.3
Size of PDF data
iOS 17: 100KB
iOS 16: 505KB
Embedded font
iOS 17: HiraginoSans-W5
iOS 16: HiraginoSans-W3
Displayed text size
iOS 17: just a little bigger than 14pt
iOS 16: 14pt
I am hoping that the printing problem can be resolved if my app running on iOS 17 creates the same PDF data as on iOS 16, so my question is:
Is there any way to create the same format of PDF data as iOS 16 or earlier when an app is running on iOS 17, especially to create data in version 1.3 of PDF?
In my app, I use almost the same as the following code to create PDF data from HTML, and I couldn't find any option to specify a version of PDF for UIPrintPageRenderer, UIMarkupTextPrintFormatter and UIGraphicsPDFContext classes...
Any info would be appreciated. Thanks,
let htmlString = """
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=420,height=595, shrink-to-fit=yes" />
<style type="text/css">
* {
box-sizing: border-box;
font-family: 'Hiragino Mincho ProN', serif;
font-size: 1em;
line-height: 1;
}
body {
margin: 0;
padding: 0;
font-size: 14pt;
}
span.gothic {
font-size: 3.5em;
font-family: "Helvetica Neue", Helvetica, "Hiragino Sans", sans-serif;
}
</style>
</head>
<body>
明朝体のテキスト
<span class="gothic">ゴシック体のテキスト</span>
</body>
</html>
"""
let render = UIPrintPageRenderer()
let paperRect = CGRect(x: 0, y: 0, width: 420.0, height: 595) // A5, 72 dpi
render.setValue(paperRect, forKey: "paperRect")
let contentsRect = CGRect(x: 0, y: 0, width: 420.0, height: 595) // A5, 72 dpi
render.setValue(contentsRect, forKey: "printableRect")
let fmt = UIMarkupTextPrintFormatter(markupText: htmlString)
render.addPrintFormatter(fmt, startingAtPageAt: 0)
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, paperRect, nil)
render.prepare(forDrawingPages: NSMakeRange(0, render.numberOfPages))
let bound = UIGraphicsGetPDFContextBounds()
for i in 0..<render.numberOfPages {
UIGraphicsBeginPDFPage()
render.drawPage(at: i, in: bound)
}
UIGraphicsEndPDFContext()
do {
let url = URL(fileURLWithPath:"test.pdf")
try pdfData.write(to: url)
print("Done")
} catch {
print("Failed to save", error)
}