Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Organize PDF #61

Closed
deon-gracias opened this issue Sep 18, 2024 · 4 comments
Closed

Feature Request: Organize PDF #61

deon-gracias opened this issue Sep 18, 2024 · 4 comments

Comments

@deon-gracias
Copy link
Contributor

I thought a cool feature would be to organize pdfs, since I use it on ilovepdf.com a lot. So I wrote this function from looking at some functions in the codebase.

The below code simply uses the existing split and merge functions to organize the pdfs.

I couldn't get it to run since I don't have a key to the product and I don't work at a company.

I hope I can contribute to the repo by making a pull request I can work on this feature. I wanted to create a function for it in the pkg folder. But I'm not sure of the behavior of the program since I have not been able to get it run due to the license.

var organizeCmd = &cobra.Command{
	Use:                   "organize [FLAG]... INPUT_FILE OUTPUT_FILE [PAGES]",
	Short:                 "organize PDF files",
	Long:                  organizeCmdDesc,
	Example:               organizeCmdExample,
	DisableFlagsInUseLine: true,
	Run: func(cmd *cobra.Command, args []string) {
		inputPath := args[0]
		outputPath := args[1]
		password, _ := cmd.Flags().GetString("password")

		// Parse page range.
		var err error
		var pages []int

		if len(args) > 2 {
			if pages, err = parsePageRange(args[2]); err != nil {
				printUsageErr(cmd, "Invalid page range specified\n")
			}
		}

		splitFiles := make([]string, len(pages), len(pages))
		for _, page := range pages {
			temporaryFilePath := "./tmp/" + inputPath + "-" + strconv.Itoa(page)
			err = pdf.Split(inputPath, temporaryFilePath, password, []int{page})

			if err != nil {
				printErr("Error: %v\n", err)
			}
		}

		err = pdf.Merge(splitFiles, outputPath)
		if err != nil {
			printErr("Error: %v\n", err)
		}

		fmt.Printf("Successfully organized file %s\n", inputPath)
		fmt.Printf("Output file saved to %s\n", outputPath)
	},
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return errors.New("must provide at least the input and output files")
		}

		return nil
	},
}
@sampila
Copy link
Contributor

sampila commented Sep 19, 2024

Hi @deon-gracias,

Thanks for the suggestion and you can get free 100 credits from registering at https://cloud.unidoc.io so you make your code run

@deon-gracias
Copy link
Contributor Author

deon-gracias commented Sep 21, 2024

I worked on this feature on my fork.
This is the commit for the changes for adding the feature.

If you wish to incorporate, please let me know if I should submit a pull request.

Here are some of the screenshots of the working.

image
image
image

@sampila
Copy link
Contributor

sampila commented Sep 22, 2024

Hi @deon-gracias,

Yes you can upon up the PR, we will reviews and adjust your PR later if needed to match our code standard.

Thanks for your contribution.

@deon-gracias
Copy link
Contributor Author

Opened a pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants