【问题标题】:MacOS render images from PDFs (no third party render lib)MacOS 从 PDF 渲染图像(没有第三方渲染库)
【发布时间】:2021-11-17 00:58:12
【问题描述】:

我正在尝试使用 Mac Mini 渲染 50k 图像。我对系统了解不多,但需要用到Preview.app之类的内置渲染器。

我目前测试设置一个 Automator 脚本,该脚本可以获取在 Finder 窗口中选择的文件、渲染图像并将它们移动到另一个目录。

但是用 50k 单页 PDF 运行该脚本是行不通的。相反,它似乎使用内存来存储 PDF 或类似的东西。

我从未编写过任何 AppleScript、Swift 或类似代码,但我精通 Bash 和 Java,所以如果我可以从命令行执行此操作,那将是理想的,但如果有人有建议关于可以解决此问题的 AppleScript,我全神贯注。

感谢您阅读本文。

【问题讨论】:

  • 要使用 applescript 将 pdf 保存为 png,一种简单的方法是首先设置对具有所需名称的文件的引用:set pdk to ((path to desktop) as text) & "outs.png",然后将预览保存到该引用中:@ 987654324@。您可以在 bash 或 applescript 中构建一种文件馈送机制,也可以使用一种来准备名称。这些 applescript 命令也可以从 bash 中调用……详情请尝试man osascript
  • 另外,您可以使用 Poppler-utils 完全留在 shell 中,特别是 pdftocairo 命令,它可以将单页转换为各种图像格式。
  • 问题是使用预览呈现 PDF。但是我已经处理了光栅化PDF文件的问题,如果OP有兴趣,我已经有一个比Preview快得多的现成脚本,并且无需使用第三方软件。
  • 这对我来说完全无法理解。也许是因为我的英语不太好。我想你想改变你原来的问题。但是,它应该设置在一个单独的主题中。或者,如果您发现我的脚本太聪明,请使用@Mockman 的答案。他对预览的回答应该有效,只是速度更慢。 50,000 张图片和 Preview.app 祝您一切顺利。
  • “pdf to png mac command line”在 Google 上的热门搜索:How to convert PDF to PNG from the command line on a Mac。另请参阅:Stack Overflow is not a code-writing service.

标签: macos pdf applescript rendering automator


【解决方案1】:
-- 1) Create --> **Quick Action** (that is, service)
-- 2) Set: **Workflow receives current PDFs in Finder.app**
-- 3) Add action **Run AppleScript** with following content.
-- 4) Save this service with name like "Render images from single_page PDFs".

use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "QuartzCore"

on run {input, parameters}
    set destinationFolder to (choose folder with prompt "Please, choose Destination Folder")
    set destFolderPosixPath to POSIX path of destinationFolder
    repeat with aPDF in (get input)
        (my rasterPDF:aPDF savingToFolder:destFolderPosixPath)
    end repeat
    return input
end run

on rasterPDF:aPDF savingToFolder:destFolderPosixPath
    set aURL to (|NSURL|'s fileURLWithPath:(POSIX path of aPDF))
    set fileName to aURL's lastPathComponent()
    set baseName to fileName's stringByDeletingPathExtension()
    set aPDFdoc to PDFDocument's alloc()'s initWithURL:aURL
    set doc to (NSImage's alloc()'s initWithData:((aPDFdoc's pageAtIndex:0)'s dataRepresentation()))
    if doc = missing value then error "Error in getting image from PDF"
    set theData to doc's TIFFRepresentation()
    set aNSBitmapImageRep to (NSBitmapImageRep's imageRepsWithData:theData)'s objectAtIndex:0
    set outNSURL to |NSURL|'s fileURLWithPath:(destFolderPosixPath & baseName & ".png")
    set outputPNGData to aNSBitmapImageRep's representationUsingType:NSPNGFileType |properties|:{NSImageCompressionFactor:0.8, NSImageProgressive:false}
    outputPNGData's writeToURL:outNSURL atomically:true
end rasterPDF:savingToFolder:

注意:1) 脚本也会从 multiple_page PDF 的第一页呈现图像。 2) 通过使用重复循环和更改 pageAtIndex 属性,有人可以调整我的脚本以呈现所有 PDF 页面中的图像。

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2017-09-17
    相关资源
    最近更新 更多