After a week, I'm pretty sure this can't be done, but I thought I'd give it one last ditch effort.
I have an API that I call that returns a Data object in the response.result.value. That data is a full fledged PDF file. For instance if I call the API with Postman, and then click on save the result, I can open that saved file with any PDF viewer.
Now, that I the data object in my viewController, I can call data.write(to: pdfURL) and it writes the file to the URL I specified.
At this point the PDF is still good, I can print that URL, and then in finder locate that file and again view it with any PDF viewer, no problem.
Here's where the problem happens. Lastly, I want to send that PDF file to a small label printer, the labels in the printer are 2" wide, and 1" high. The PDF file that I am getting from the API, creates the PDF in 8.5" x 11" size.
So when it is printed on the label, it shrinks the file to fit on the label making the text and images on the label so small that it can't be read.
I can't change the PDF coming from the API. I can't change any more settings on the printer.
The only chance I have to "zoom in" on this is to change the document or page size of the PDF file.
I have tried getting a handle to the file that is written to disk, then getting its page and then setting its bounds.
But not the page is just blank. I know that PDF cord start with x=0,y=0 in the bottom left corner, so I've used both of these as my page size:
let page = CGRect(x: 0, y: 0, width: 85, height: 40)
let page = CGRect(x: 472, y: 707, width: 85, height: 40)
guard let data = response.result.value else {
print("Did not get PDF data from API")
return
}
let page = CGRect(x: 472, y: 707, width: 85, height: 40)
let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = "shelfTag.pdf"
let pdfURL = documentURL.appendingPathComponent("\(fileName)")
do {
print(pdfURL)
try data.write(to: pdfURL)
} catch {
print("Couldn't write PDF to file.)
}
if #available(iOS 11.0, *) {
let pdfDocument = PDFDocument(url: pdfURL)
let pdfPage = pdfDocument?.page(at: 0)
pdfPage?.setBounds(page, for: .mediaBox)
pdfDocument?.write(toFile: pdfURL.path)
} else {
// Fallback on earlier versions
}
Either the image and text is too small to read or The printed label is just blank.