github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/cli.go (about) 1 /* 2 Copyright 2019 The pdfcpu Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package cli provides pdfcpu command line processing. 18 package cli 19 20 import ( 21 "github.com/pdfcpu/pdfcpu/pkg/api" 22 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 23 ) 24 25 // Validate inFile against ISO-32000-1:2008. 26 func Validate(cmd *Command) ([]string, error) { 27 return nil, api.ValidateFiles(cmd.InFiles, cmd.Conf) 28 } 29 30 // Optimize inFile and write result to outFile. 31 func Optimize(cmd *Command) ([]string, error) { 32 return nil, api.OptimizeFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 33 } 34 35 // Encrypt inFile and write result to outFile. 36 func Encrypt(cmd *Command) ([]string, error) { 37 return nil, api.EncryptFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 38 } 39 40 // Decrypt inFile and write result to outFile. 41 func Decrypt(cmd *Command) ([]string, error) { 42 return nil, api.DecryptFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 43 } 44 45 // ChangeUserPassword of inFile and write result to outFile. 46 func ChangeUserPassword(cmd *Command) ([]string, error) { 47 return nil, api.ChangeUserPasswordFile(*cmd.InFile, *cmd.OutFile, *cmd.PWOld, *cmd.PWNew, cmd.Conf) 48 } 49 50 // ChangeOwnerPassword of inFile and write result to outFile. 51 func ChangeOwnerPassword(cmd *Command) ([]string, error) { 52 return nil, api.ChangeOwnerPasswordFile(*cmd.InFile, *cmd.OutFile, *cmd.PWOld, *cmd.PWNew, cmd.Conf) 53 } 54 55 // ListPermissions of inFile. 56 func ListPermissions(cmd *Command) ([]string, error) { 57 return ListPermissionsFile(cmd.InFiles, cmd.Conf) 58 } 59 60 // SetPermissions of inFile. 61 func SetPermissions(cmd *Command) ([]string, error) { 62 return nil, api.SetPermissionsFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 63 } 64 65 // Split inFile into single page PDFs and write result files to outDir. 66 func Split(cmd *Command) ([]string, error) { 67 return nil, api.SplitFile(*cmd.InFile, *cmd.OutDir, cmd.IntVal, cmd.Conf) 68 } 69 70 // Split inFile along pages and write result files to outDir. 71 func SplitByPageNr(cmd *Command) ([]string, error) { 72 return nil, api.SplitByPageNrFile(*cmd.InFile, *cmd.OutDir, cmd.IntVals, cmd.Conf) 73 } 74 75 // Trim inFile and write result to outFile. 76 func Trim(cmd *Command) ([]string, error) { 77 return nil, api.TrimFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Conf) 78 } 79 80 // Rotate selected pages of inFile and write result to outFile. 81 func Rotate(cmd *Command) ([]string, error) { 82 return nil, api.RotateFile(*cmd.InFile, *cmd.OutFile, cmd.IntVal, cmd.PageSelection, cmd.Conf) 83 } 84 85 // AddWatermarks adds watermarks or stamps to selected pages of inFile and writes the result to outFile. 86 func AddWatermarks(cmd *Command) ([]string, error) { 87 return nil, api.AddWatermarksFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Watermark, cmd.Conf) 88 } 89 90 // RemoveWatermarks remove watermarks or stamps from selected pages of inFile and writes the result to outFile. 91 func RemoveWatermarks(cmd *Command) ([]string, error) { 92 return nil, api.RemoveWatermarksFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Conf) 93 } 94 95 // NUp renders selected PDF pages or image files to outFile in n-up fashion. 96 func NUp(cmd *Command) ([]string, error) { 97 return nil, api.NUpFile(cmd.InFiles, *cmd.OutFile, cmd.PageSelection, cmd.NUp, cmd.Conf) 98 } 99 100 // Booklet arranges selected PDF pages to outFile in an order and arrangement that form a small book. 101 func Booklet(cmd *Command) ([]string, error) { 102 return nil, api.BookletFile(cmd.InFiles, *cmd.OutFile, cmd.PageSelection, cmd.NUp, cmd.Conf) 103 } 104 105 // ImportImages appends PDF pages containing images to outFile which will be created if necessary. 106 // ImportImages turns image files into a page sequence and writes the result to outFile. 107 // In its simplest form this operation converts an image into a PDF. 108 func ImportImages(cmd *Command) ([]string, error) { 109 return nil, api.ImportImagesFile(cmd.InFiles, *cmd.OutFile, cmd.Import, cmd.Conf) 110 } 111 112 // InsertPages inserts a blank page before or after each selected page. 113 func InsertPages(cmd *Command) ([]string, error) { 114 before := true 115 if cmd.Mode == model.INSERTPAGESAFTER { 116 before = false 117 } 118 return nil, api.InsertPagesFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, before, cmd.PageConf, cmd.Conf) 119 } 120 121 // RemovePages removes selected pages. 122 func RemovePages(cmd *Command) ([]string, error) { 123 return nil, api.RemovePagesFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Conf) 124 } 125 126 // MergeCreate merges inFiles in the order specified and writes the result to outFile. 127 func MergeCreate(cmd *Command) ([]string, error) { 128 return nil, api.MergeCreateFile(cmd.InFiles, *cmd.OutFile, cmd.BoolVal1, cmd.Conf) 129 } 130 131 // MergeCreateZip zips two inFiles in the order specified and writes the result to outFile. 132 func MergeCreateZip(cmd *Command) ([]string, error) { 133 return nil, api.MergeCreateZipFile(cmd.InFiles[0], cmd.InFiles[1], *cmd.OutFile, cmd.Conf) 134 } 135 136 // MergeAppend merges inFiles in the order specified and writes the result to outFile. 137 func MergeAppend(cmd *Command) ([]string, error) { 138 return nil, api.MergeAppendFile(cmd.InFiles, *cmd.OutFile, cmd.BoolVal1, cmd.Conf) 139 } 140 141 // ExtractImages dumps embedded image resources from inFile into outDir for selected pages. 142 func ExtractImages(cmd *Command) ([]string, error) { 143 return nil, api.ExtractImagesFile(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Conf) 144 } 145 146 // ExtractFonts dumps embedded fontfiles from inFile into outDir for selected pages. 147 func ExtractFonts(cmd *Command) ([]string, error) { 148 return nil, api.ExtractFontsFile(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Conf) 149 } 150 151 // ExtractPages generates single page PDF files from inFile in outDir for selected pages. 152 func ExtractPages(cmd *Command) ([]string, error) { 153 return nil, api.ExtractPagesFile(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Conf) 154 } 155 156 // ExtractContent dumps "PDF source" files from inFile into outDir for selected pages. 157 func ExtractContent(cmd *Command) ([]string, error) { 158 return nil, api.ExtractContentFile(*cmd.InFile, *cmd.OutDir, cmd.PageSelection, cmd.Conf) 159 } 160 161 // ExtractMetadata dumps all metadata dict entries for inFile into outDir. 162 func ExtractMetadata(cmd *Command) ([]string, error) { 163 return nil, api.ExtractMetadataFile(*cmd.InFile, *cmd.OutDir, cmd.Conf) 164 } 165 166 // ListAttachments returns a list of embedded file attachments for inFile. 167 func ListAttachments(cmd *Command) ([]string, error) { 168 return ListAttachmentsFile(*cmd.InFile, cmd.Conf) 169 } 170 171 // AddAttachments embeds inFiles into a PDF context read from inFile and writes the result to outFile. 172 func AddAttachments(cmd *Command) ([]string, error) { 173 return nil, api.AddAttachmentsFile(*cmd.InFile, *cmd.OutFile, cmd.InFiles, cmd.Mode == model.ADDATTACHMENTSPORTFOLIO, cmd.Conf) 174 } 175 176 // RemoveAttachments deletes inFiles from a PDF context read from inFile and writes the result to outFile. 177 func RemoveAttachments(cmd *Command) ([]string, error) { 178 return nil, api.RemoveAttachmentsFile(*cmd.InFile, *cmd.OutFile, cmd.InFiles, cmd.Conf) 179 } 180 181 // ExtractAttachments extracts inFiles from a PDF context read from inFile and writes the result to outFile. 182 func ExtractAttachments(cmd *Command) ([]string, error) { 183 return nil, api.ExtractAttachmentsFile(*cmd.InFile, *cmd.OutDir, cmd.InFiles, cmd.Conf) 184 } 185 186 // ListInfo gathers information about inFile and returns the result as []string. 187 func ListInfo(cmd *Command) ([]string, error) { 188 return ListInfoFiles(cmd.InFiles, cmd.PageSelection, cmd.BoolVal1, cmd.BoolVal2, cmd.Conf) 189 } 190 191 // CreateCheatSheetsFonts creates single page PDF cheat sheets for user fonts in current dir. 192 func CreateCheatSheetsFonts(cmd *Command) ([]string, error) { 193 return nil, api.CreateCheatSheetsUserFonts(cmd.InFiles) 194 } 195 196 // ListFonts gathers information about supported fonts and returns the result as []string. 197 func ListFonts(cmd *Command) ([]string, error) { 198 return api.ListFonts() 199 } 200 201 // InstallFonts installs True Type fonts into the pdfcpu pconfig dir. 202 func InstallFonts(cmd *Command) ([]string, error) { 203 return nil, api.InstallFonts(cmd.InFiles) 204 } 205 206 // ListKeywords returns a list of keywords for inFile. 207 func ListKeywords(cmd *Command) ([]string, error) { 208 return ListKeywordsFile(*cmd.InFile, cmd.Conf) 209 } 210 211 // AddKeywords adds keywords to inFile's document info dict and writes the result to outFile. 212 func AddKeywords(cmd *Command) ([]string, error) { 213 return nil, api.AddKeywordsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 214 } 215 216 // RemoveKeywords deletes keywords from inFile's document info dict and writes the result to outFile. 217 func RemoveKeywords(cmd *Command) ([]string, error) { 218 return nil, api.RemoveKeywordsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 219 } 220 221 // ListProperties returns inFile's properties. 222 func ListProperties(cmd *Command) ([]string, error) { 223 return ListPropertiesFile(*cmd.InFile, cmd.Conf) 224 } 225 226 // AddProperties adds properties to inFile's document info dict and writes the result to outFile. 227 func AddProperties(cmd *Command) ([]string, error) { 228 return nil, api.AddPropertiesFile(*cmd.InFile, *cmd.OutFile, cmd.StringMap, cmd.Conf) 229 } 230 231 // RemoveProperties deletes properties from inFile's document info dict and writes the result to outFile. 232 func RemoveProperties(cmd *Command) ([]string, error) { 233 return nil, api.RemovePropertiesFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 234 } 235 236 // Collect creates a custom page sequence for selected pages of inFile and writes result to outFile. 237 func Collect(cmd *Command) ([]string, error) { 238 return nil, api.CollectFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Conf) 239 } 240 241 // ListBoxes returns inFile's page boundaries. 242 func ListBoxes(cmd *Command) ([]string, error) { 243 return ListBoxesFile(*cmd.InFile, cmd.PageSelection, cmd.PageBoundaries, cmd.Conf) 244 } 245 246 // AddBoxes adds page boundaries to inFile's page tree and writes the result to outFile. 247 func AddBoxes(cmd *Command) ([]string, error) { 248 return nil, api.AddBoxesFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.PageBoundaries, cmd.Conf) 249 } 250 251 // RemoveBoxes deletes page boundaries from inFile's page tree and writes the result to outFile. 252 func RemoveBoxes(cmd *Command) ([]string, error) { 253 return nil, api.RemoveBoxesFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.PageBoundaries, cmd.Conf) 254 } 255 256 // Crop adds crop boxes for selected pages of inFile and writes result to outFile. 257 func Crop(cmd *Command) ([]string, error) { 258 return nil, api.CropFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Box, cmd.Conf) 259 } 260 261 // ListAnnotations returns inFile's page annotations. 262 func ListAnnotations(cmd *Command) ([]string, error) { 263 _, ss, err := ListAnnotationsFile(*cmd.InFile, cmd.PageSelection, cmd.Conf) 264 return ss, err 265 } 266 267 // RemoveAnnotations deletes annotations from inFile's page tree and writes the result to outFile. 268 func RemoveAnnotations(cmd *Command) ([]string, error) { 269 incr := false // No incremental writing on cli. 270 return nil, api.RemoveAnnotationsFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.StringVals, cmd.IntVals, cmd.Conf, incr) 271 } 272 273 // ListImages returns inFiles embedded images. 274 func ListImages(cmd *Command) ([]string, error) { 275 return ListImagesFile(cmd.InFiles, cmd.PageSelection, cmd.Conf) 276 } 277 278 // UpdateImages replaces image objects. 279 func UpdateImages(cmd *Command) ([]string, error) { 280 var ( 281 objNr int 282 pageNr int 283 id string 284 ) 285 if cmd.IntVal > 0 { 286 if cmd.StringVal != "" { 287 pageNr = cmd.IntVal 288 id = cmd.StringVal 289 } else { 290 objNr = cmd.IntVal 291 } 292 } 293 return nil, api.UpdateImagesFile(cmd.InFiles[0], cmd.InFiles[1], *cmd.OutFile, objNr, pageNr, id, cmd.Conf) 294 } 295 296 // Dump known object to stdout. 297 func Dump(cmd *Command) ([]string, error) { 298 mode := cmd.IntVals[0] 299 objNr := cmd.IntVals[1] 300 return nil, api.DumpObjectFile(*cmd.InFile, mode, objNr, cmd.Conf) 301 } 302 303 // Create renders page content corresponding to declarations found in inFileJSON and writes the result to outFile. 304 // If inFile is present, page content will be appended, 305 func Create(cmd *Command) ([]string, error) { 306 return nil, api.CreateFile(*cmd.InFile, *cmd.InFileJSON, *cmd.OutFile, cmd.Conf) 307 } 308 309 // ListFormFields returns inFile's form field ids. 310 func ListFormFields(cmd *Command) ([]string, error) { 311 return ListFormFieldsFile(cmd.InFiles, cmd.Conf) 312 } 313 314 // RemoveFormFields removes some form fields from inFile. 315 func RemoveFormFields(cmd *Command) ([]string, error) { 316 return nil, api.RemoveFormFieldsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 317 } 318 319 // LockFormFields makes some or all form fields of inFile read-only. 320 func LockFormFields(cmd *Command) ([]string, error) { 321 return nil, api.LockFormFieldsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 322 } 323 324 // UnlockFormFields makes some or all form fields of inFile writeable. 325 func UnlockFormFields(cmd *Command) ([]string, error) { 326 return nil, api.UnlockFormFieldsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 327 } 328 329 // ResetFormFields sets some or all form fields of inFile to the corresponding default value. 330 func ResetFormFields(cmd *Command) ([]string, error) { 331 return nil, api.ResetFormFieldsFile(*cmd.InFile, *cmd.OutFile, cmd.StringVals, cmd.Conf) 332 } 333 334 // ExportFormFields returns a representation of inFile's form as outFileJSON. 335 func ExportFormFields(cmd *Command) ([]string, error) { 336 return nil, api.ExportFormFile(*cmd.InFile, *cmd.OutFileJSON, cmd.Conf) 337 } 338 339 // FillFormFields fills out inFile's form using data represented by inFileJSON. 340 func FillFormFields(cmd *Command) ([]string, error) { 341 return nil, api.FillFormFile(*cmd.InFile, *cmd.InFileJSON, *cmd.OutFile, cmd.Conf) 342 } 343 344 // MultiFillFormFields fills out multiple instances of inFile's form using JSON or CSV data. 345 func MultiFillFormFields(cmd *Command) ([]string, error) { 346 return nil, api.MultiFillFormFile(*cmd.InFile, *cmd.InFileJSON, *cmd.OutDir, *cmd.OutFile, cmd.BoolVal1, cmd.Conf) 347 } 348 349 // Resize selected pages and write result to outFile. 350 func Resize(cmd *Command) ([]string, error) { 351 return nil, api.ResizeFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Resize, cmd.Conf) 352 } 353 354 // Create poster for selected pages and write result PDFs into outDir. 355 func Poster(cmd *Command) ([]string, error) { 356 return nil, api.PosterFile(*cmd.InFile, *cmd.OutDir, *cmd.OutFile, cmd.PageSelection, cmd.Cut, cmd.Conf) 357 } 358 359 // NDown selected pages and write result PDFs into outDir. 360 func NDown(cmd *Command) ([]string, error) { 361 return nil, api.NDownFile(*cmd.InFile, *cmd.OutDir, *cmd.OutFile, cmd.PageSelection, cmd.IntVal, cmd.Cut, cmd.Conf) 362 } 363 364 // Cut selected pages and write result PDFs into outDir. 365 func Cut(cmd *Command) ([]string, error) { 366 return nil, api.CutFile(*cmd.InFile, *cmd.OutDir, *cmd.OutFile, cmd.PageSelection, cmd.Cut, cmd.Conf) 367 } 368 369 // ListBookmarks returns inFile's outlines. 370 func ListBookmarks(cmd *Command) ([]string, error) { 371 return ListBookmarksFile(*cmd.InFile, cmd.Conf) 372 } 373 374 // ExportBookmarks returns a representation of inFile's outlines as outFileJSON. 375 func ExportBookmarks(cmd *Command) ([]string, error) { 376 return nil, api.ExportBookmarksFile(*cmd.InFile, *cmd.OutFileJSON, cmd.Conf) 377 } 378 379 // ImportBookmarks creates/replaces outlines of inFile corresponding to declarations found in inJSONFile and writes the result to outFile. 380 func ImportBookmarks(cmd *Command) ([]string, error) { 381 return nil, api.ImportBookmarksFile(*cmd.InFile, *cmd.InFileJSON, *cmd.OutFile, cmd.BoolVal1, cmd.Conf) 382 } 383 384 // RemoveBookmarks erases outlines of inFile. 385 func RemoveBookmarks(cmd *Command) ([]string, error) { 386 return nil, api.RemoveBookmarksFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 387 } 388 389 // ListPageLayout returns inFile's page layout. 390 func ListPageLayout(cmd *Command) ([]string, error) { 391 return api.ListPageLayoutFile(*cmd.InFile, cmd.Conf) 392 } 393 394 // SetPageLayout sets inFile's page layout. 395 func SetPageLayout(cmd *Command) ([]string, error) { 396 pageLayout := model.PageLayoutFor(cmd.StringVal) 397 return nil, api.SetPageLayoutFile(*cmd.InFile, *cmd.OutFile, *pageLayout, cmd.Conf) 398 } 399 400 // ResetPageLayout resets inFile's page layout. 401 func ResetPageLayout(cmd *Command) ([]string, error) { 402 return nil, api.ResetPageLayoutFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 403 } 404 405 // ListPageMode returns inFile's page mode. 406 func ListPageMode(cmd *Command) ([]string, error) { 407 return api.ListPageModeFile(*cmd.InFile, cmd.Conf) 408 } 409 410 // SetPageMode sets inFile's page mode. 411 func SetPageMode(cmd *Command) ([]string, error) { 412 pageMode := model.PageModeFor(cmd.StringVal) 413 return nil, api.SetPageModeFile(*cmd.InFile, *cmd.OutFile, *pageMode, cmd.Conf) 414 } 415 416 // ResetPageMode resets inFile's page mode. 417 func ResetPageMode(cmd *Command) ([]string, error) { 418 return nil, api.ResetPageModeFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 419 } 420 421 // ListViewerPreferences returns inFile's viewer preferences. 422 func ListViewerPreferences(cmd *Command) ([]string, error) { 423 return api.ListViewerPreferencesFile(*cmd.InFile, cmd.BoolVal1, cmd.BoolVal2, cmd.Conf) 424 } 425 426 // SetViewerPreferences sets inFile's viewer preferences. 427 func SetViewerPreferences(cmd *Command) ([]string, error) { 428 if *cmd.InFileJSON != "" { 429 return nil, api.SetViewerPreferencesFileFromJSONFile(*cmd.InFile, *cmd.OutFile, *cmd.InFileJSON, cmd.Conf) 430 } 431 return nil, api.SetViewerPreferencesFileFromJSONBytes(*cmd.InFile, *cmd.OutFile, []byte(cmd.StringVal), cmd.Conf) 432 } 433 434 // ResetViewerPreferences resets inFile's viewer preferences. 435 func ResetViewerPreferences(cmd *Command) ([]string, error) { 436 return nil, api.ResetViewerPreferencesFile(*cmd.InFile, *cmd.OutFile, cmd.Conf) 437 } 438 439 // Zoom in/out of selected pages either by zoom factor or corresponding margin. 440 func Zoom(cmd *Command) ([]string, error) { 441 return nil, api.ZoomFile(*cmd.InFile, *cmd.OutFile, cmd.PageSelection, cmd.Zoom, cmd.Conf) 442 } 443 444 // ListCertificates returns installed certificates. 445 func ListCertificates(cmd *Command) ([]string, error) { 446 return ListCertificatesAll(cmd.BoolVal1, cmd.Conf) 447 } 448 449 // ListCertificates returns installed certificates. 450 func ImportCertificates(cmd *Command) ([]string, error) { 451 return api.ImportCertificates(cmd.InFiles) 452 } 453 454 // InspectCertificates prints the certificate details. 455 func InspectCertificates(cmd *Command) ([]string, error) { 456 return api.InspectCertificates(cmd.InFiles) 457 } 458 459 // ValidateSignatures validates contained digital signatures. 460 func ValidateSignatures(cmd *Command) ([]string, error) { 461 return api.ValidateSignaturesFile(*cmd.InFile, cmd.BoolVal1, cmd.BoolVal2, cmd.Conf) 462 }