Reputation: 327
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.
Upvotes: 1
Views: 2394
Reputation: 1033
This looks very promising to me: https://github.com/prachigauriar/ResizePDF
The author describes it this way:
"resizepdf
is a tool for macOS that resizes PDFs! It’s written in Swift and is very simple. It has no external dependencies."
In my brief experimentation with it I found that although it's a Mac app intended as a command line tool I think it can be easily migrated to iOS.
I found that the app works but trivial bugs keep it from running right off the bat. (The test that runs on startup looks for a filename that is different from the one the asset has. Match that name on the asset to achieve success. It is not necessary to run the app as a command line tool -- it'll kick off a test task when run in Xcode.)
Upvotes: 0
Reputation: 548
PDF functionality in iOS is limited. You could use these classes to recreate a PDF of the desired size if you have access to the data on the PDF. https://developer.apple.com/documentation/uikit/1623915-uigraphicsbeginpdfpagewithinfo?language=objc
The other option is to use a 3rd party app like Foxit. There may be a free versions you could use.
Unfortunately, there is no easy answer!
Upvotes: 1