Resize pdf document downloaded from API in Swift - swift

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.

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!

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.)

Related

Correct icon size for Big Sur preference panels

When building my app for macOS Big Sur, and switching my preference panel toolbar style to .preference, the icons just don't seem to size correctly.
No matter if I use #1x/#2x PNG variants, or universal PDF vector images with a single scale, my icons always turn out larger and blurry compared to using a SF Symbol icon.
Now that my app needs to be backwards compatible with 10.13 I can't use symbol images.
Here is what my toolbar looks like
What is the correct way to get properly sized icons in a preference panel toolbar?
Do I need to include a margin in my vector assets? If so, what is the correct point size?
I got a good result with 26x26px vector PDF. The icon size was 22x22px with the margins but the icon was blurry. Enabling Preserve Vector Data option did the trick
You can set it under the Image Set settings:
Vector images don't seem to go well with NSToolbar. I created an NSImage extension for them to manually create a toolbar friendly image.
extension NSImage {
convenience init?(namedVectorToolbarImage name: String) {
guard let image = NSImage(named: name) else {
return nil
}
self.init()
let small = image.resized(to: .init(width: 24, height: 24))
let regular = image.resized(to: .init(width: 32, height: 32))
let reps = regular.representations + small.representations
isTemplate = true
reps.forEach {
addRepresentation($0)
}
}
The resize() method here is just another extension for producing a resized image. On top of that I set NSToolbar.sizeMode to small if the system is not (below) macOS 11.0.

how to use .svg images in SwiftUI

Can I only upload .png files into the assets folder? I get an error for .svg files.
I want to use it for the Image() component
You can use Macaw to display a SVG: https://github.com/exyte/Macaw
1) Add the Package via SPM to your project (the tag 0.9.5 actually doesn't work, you need to use the master branch)
2) Move your .svg-file to the project or create a new Data set in your Assets.xcassets and then add the svg file to this data set.
3) Use this code:
struct MyView: View {
var svgName: String
var body: some View {
SVGImage(svgName: self.svgName)
.frame(width: 50, height: 550)
}
}
struct SVGImage: UIViewRepresentable {
var svgName: String
func makeUIView(context: Context) -> SVGView {
let svgView = SVGView()
svgView.backgroundColor = UIColor(white: 1.0, alpha: 0.0) // otherwise the background is black
svgView.fileName = self.svgName
svgView.contentMode = .scaleToFill
return svgView
}
func updateUIView(_ uiView: SVGView, context: Context) {
}
}
You can import svg images too in XCAssets.
Go to XCAssets, click on + button.
Refer this image
Click on import & select svg image.
Now to load & display that image use below code :
Image(uiImage: UIImage(named: "imageName")!)
I've created a simple tool, which converts SVG code into SwiftUI's Shape object. It is very limited for now (in particular it only supports straight lines and cubic curves, single path SVGs and shapes that are filled with one color), but you can still use it for simple situations.
Here's the link to the tool, and the repository, in case you'd want to contribute and make it support all SVG features.
You can use pdf's for vectors in the asset catalog (select single scale and preserve vector data in the inspector on the right). You cannot directly use SVG's in the asset catalog, but with Xcode 11 you actually can use SVG's for symbols. Apple has a detailed guide on how to make your own SVG symbols here.

How to fix repeating canvas.toBuffer or canvas.createPdfStream content

I'm using canvas.toBuffer in a loop to generate pdf for different content but all the generated pdf's have the same content. Below is a snippet of the loop I'm using.
for (const d of data) {
context.clearRect(0, 0, imageWidth, imageHeight);
context.drawImage(image, 0, 0, imageWidth, imageHeight);
context.fillText(d.name.toUpperCase(), 1685, 1556);
fs.writeFileSync(`./assets/certs/${d.email}.pdf`,canvas.toBuffer());
}
data is of type, Array<{name:String, email:String}>.
The name is supposed to print the name of each user on an image but It turns out all the pdfs generated have just the name of the first user.
I tried creating a ReadStream from the canvas but I end up with the same issue. Is there something I'm doing wrong or am I missing something ?
This is sort of a bug in node-canvas. It's not possible to reuse a PDF or SVG canvas after calling toBuffer() on it (https://github.com/Automattic/node-canvas/issues/1195).
As a work-around, you can make a new canvas in each iteration of the loop.

NSCollectionView Grid of Files Displayed with Icons from Finder's Preview

I am having trouble trying to create an NSCollectionView with icons that are taken from Finder's icon preview.
Ideally, if the file is a dmg it should display the hard drive icon, an mp3 should be the mp3 icon and a photo should display what the photo looks like itself.
There is one post that addresses this issue:
Is it possible to get programmatic access to finder preview images?
But the code is old and does not compile. I am also using Swift in this project.
I used this post to setup a template for my project:
Is there a way to set up a NSCollectionView programmatically in Swift?
Which adds a subview to NSView. If this can be modified to implement the icons, that would be ideal.
In addition I am using a NSFileManager.defaultManager to get the contents of a folder, and would like to display those contents in the collection view.
I am new to developing on the Mac platform and have more experience with iOS, specific answers are much appreciated, thanks!
Update, I was able to figure out how to use what Finder uses with the Quicklook library that Apple provides. Here is part of the code that I used. Note that if you are trying to use Quicklook on a file that does not have a preview, then there will be an error, so in those cases I just used the iconForFile function (below this code block)
let tmp = QLThumbnailImageCreate(kCFAllocatorDefault, tmpURL , CGSize(width: 64, height: 64), nil)
tmpImage = tmp.takeUnretainedValue()
tmpIcon = NSImage(CGImage: tmpImage!, size: NSSize(width: 64, height: 64))
Original:
I was able to find (almost) what I was looking for.
In the NSWorkspace class there is a method
func iconForFile(_ fullPath: String) -> NSImage
That will return an NSImage of the icon. Unfortunately, I do not see a way to get a preview of the image if it is a picture. Nor do I see a way to make the picture larger (only 32x32 pixels).
Hopefully this answer will help others that encounter this problem!
I've used your code from your answer but I noticed that it did not keep the file's aspect ratio and the low res version would just be the downsampled high res version. So based on your answer, here's my solution:
let imageLowResRef = QLThumbnailImageCreate(kCFAllocatorDefault, url as CFURL , CGSize(width: 32, height: 32), nil)
guard imageLowResRef != nil else {
return NSWorkspace.shared.icon(forFile: url.path)
}
let image = NSImage()
let imageLowRes = imageLowResRef!.takeUnretainedValue()
image.addRepresentation(NSBitmapImageRep(cgImage: imageLowRes))
let imageHighResRef = QLThumbnailImageCreate(kCFAllocatorDefault, url as CFURL , CGSize(width: imageLowRes.width*2, height: imageLowRes.height*2), nil)
let imageHighRes = imageHighResRef!.takeUnretainedValue()
image.addRepresentation(NSBitmapImageRep(cgImage: imageHighRes))
Edit: If you add let options = [kQLThumbnailOptionIconModeKey: true] as CFDictionary, you get to the options parameter, you get the preview rendered as icons.

Generate Custom UIApplicationShortcutIcon without bundling the image.

I currently have UIApplicationShortcutItem working, however, I would like to use my own custom Icon.
I know that I can use iconWithTemplateImageName to get my own custom icon. However, I would rather not include 100s of icons with the app. It seems that iconWithTemplateImageName requires the image file to be included in the xcassets.
I have a function that downloads and save an image to the Document folder upon the user dynamically adding UIApplicationShortcutItem. Is there a way to referance the newly downloaded image.
How I add UIApplicationShortcutItem
let icon = UIApplicationShortcutIcon(templateImageName: fileName)
let newItem = UIMutableApplicationShortcutItem(type: "Chance", localizedTitle: title, localizedSubtitle: chances.joinWithSeparator(", "), icon: icon, userInfo: ["Items":chances])
existingShortcutItems.append(newItem)
UIApplication.sharedApplication().shortcutItems = existingShortcutItems
Where fileName is the path to an image I have saved. I have tried both the full path and just the file name. Ofcourse that wont work since its not in the xcassets
Is there a way to add the image to the xcassets at runtime?
Is there a way to use an image on the fly?
Working version of the code:
let icon = UIApplicationShortcutIcon(type: .Favorite)
let newItem = UIMutableApplicationShortcutItem(type: "Chance", localizedTitle: title, localizedSubtitle: chances.joinWithSeparator(", "), icon: icon, userInfo: ["Items":chances])
existingShortcutItems.append(newItem)
UIApplication.sharedApplication().shortcutItems = existingShortcutItems