app.pdf - PDF Operations API
PDF file info retrieval, text extraction, merging, splitting, and image export.
Basic Info
app.pdf.info(path)
Get PDF file information.
Parameters:
path(string) - PDF file path
Returns: table, error - {pageCount, title?, author?, subject?, creator?, keywords?}
local info, err = app.pdf.info("/path/to/document.pdf")
if info then
app.log.info("Page count: " .. info.pageCount)
app.log.info("Title: " .. (info.title or "None"))
end
app.pdf.getText(path, page?)
Extract text content from a PDF.
Parameters:
path(string) - PDF file pathpage(number, optional) - page number (starting from 1). Omit to extract all pages
Returns: string, error
-- Extract all text
local text, err = app.pdf.getText("/path/to/document.pdf")
-- Extract page 3
local text, err = app.pdf.getText("/path/to/document.pdf", 3)
Merge and Split
app.pdf.merge(paths, outputPath)
Merge multiple PDF files.
Parameters:
paths(array) - array of PDF file paths (at least 2)outputPath(string) - output file path
Returns: boolean, error
local ok, err = app.pdf.merge({
"/path/to/doc1.pdf",
"/path/to/doc2.pdf",
"/path/to/doc3.pdf"
}, "/path/to/merged.pdf")
app.pdf.split(path, outputDir, range?)
Split a PDF file.
Parameters:
path(string) - PDF file pathoutputDir(string) - output directoryrange(string, optional) - page range (e.g.,"1-3,5,7-9", starting from 1). Omit to create one file per page
Returns: boolean, error
-- One file per page
app.pdf.split("/path/to/doc.pdf", "/path/to/output/")
-- Produces: doc_page1.pdf, doc_page2.pdf, ...
-- Split by specified range
app.pdf.split("/path/to/doc.pdf", "/path/to/output/", "1-3,5,7-9")
-- Produces: doc_1-3.pdf, doc_5.pdf, doc_7-9.pdf
app.pdf.extractPages(path, outputPath, pages)
Extract specified pages to a new PDF.
Parameters:
path(string) - source PDF file pathoutputPath(string) - output file pathpages(string) - page range (e.g.,"1-3,5,7-9", starting from 1)
Returns: boolean, error
app.pdf.extractPages("/path/to/doc.pdf", "/path/to/extract.pdf", "1-5,10")
Image Export
app.pdf.toImages(path, outputDir, options?)
Export PDF pages as images.
Parameters:
path(string) - PDF file pathoutputDir(string) - output directoryoptions(table, optional):format(string) - image format:"png"(default),"jpg","tiff","gif","bmp"dpi(number) - resolution (default 150)pages(string) - page range (e.g.,"1-3"). Omit to export all pages
Returns: boolean, error
-- Export all pages as PNG
app.pdf.toImages("/path/to/doc.pdf", "/path/to/images/")
-- High-resolution JPEG, specific pages
app.pdf.toImages("/path/to/doc.pdf", "/path/to/images/", {
format = "jpg",
dpi = 300,
pages = "1-5"
})
Examples
PDF Merge Tool
function MyPlugin:handleMergePDF(context)
local pdfFiles = {}
for _, file in ipairs(context.selectedFiles) do
if app.path.extension(file) == "pdf" then
table.insert(pdfFiles, file)
end
end
if #pdfFiles < 2 then
app.dialog.alert("Note", "Please select at least 2 PDF files")
return
end
local outputPath = app.path.join(context.currentDirectory, "merged.pdf")
local ok, err = app.pdf.merge(pdfFiles, outputPath)
if ok then
app.notification.show("Done", "Merged " .. #pdfFiles .. " PDFs")
app.finder.reveal(outputPath)
else
app.dialog.alert("Error", err or "Merge failed")
end
end