Skip to content

Commit

Permalink
MAINT: Adjust x2pdf syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Sep 17, 2023
1 parent 93204ee commit 6241026
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ pdfly --help
│ extract-text Extract text from a PDF file. │
│ meta Show metadata of a PDF file │
│ pagemeta Give details about a single page. │
│ x2pdf Convert a file to PDF.
│ x2pdf Convert one or more files to PDF. Each file is a page.
╰─────────────────────────────────────────────────────────────────────────────╯
```

Expand Down
43 changes: 37 additions & 6 deletions docs/user/subcommand-x2pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,48 @@ Currently supported for "x":
## Usage

```
pdfly x2pdf --help
$ pdfly x2pdf --help
Usage: pdfly x2pdf [OPTIONS] X OUTPUT
Usage: pdfly x2pdf [OPTIONS] X...
Convert a file to PDF.
Convert one or more files to PDF. Each file is a page.
╭─ Arguments ─────────────────────────────────────────────────────────────────╮
│ * x FILE [default: None] [required] │
│ * output PATH [default: None] [required] │
│ * x X... [default: None] [required] │
╰─────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
│ * --output -o PATH [default: None] [required] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────╯
```

## Examples

### Single file

```
$ pdfly x2pdf image.jpg -o out.pdf
$ ls -lh
-rw-rw-r-- 1 user user 47K Sep 17 21:49 image.jpg
-rw-rw-r-- 1 user user 49K Sep 17 22:48 out.pdf
```

### Multiple files manually

```
$ pdfly x2pdf image1.jpg image2.jgp -o out.pdf
$ ls -lh
-rw-rw-r-- 1 user user 47K Sep 17 21:49 image1.jpg
-rw-rw-r-- 1 user user 15K Sep 17 21:49 image2.jpg
-rw-rw-r-- 1 user user 64K Sep 17 22:48 out.pdf
```

### Multiple files via *

```
$ pdfly x2pdf *.jpg -o out.pdf
$ ls -lh
-rw-rw-r-- 1 user user 47K Sep 17 21:49 image1.jpg
-rw-rw-r-- 1 user user 15K Sep 17 21:49 image2.jpg
-rw-rw-r-- 1 user user 64K Sep 17 22:48 out.pdf
```
24 changes: 14 additions & 10 deletions pdfly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,29 @@ def compress(
resolve_path=True,
),
],
output: Path,
output: Annotated[
Path,
typer.Argument(
exists=False,
writable=True,
),
],
) -> None:
pdfly.compress.main(pdf, output)


@entry_point.command(name="x2pdf") # type: ignore[misc]
def x2pdf(
x: Annotated[
x: List[Path],
output: Annotated[
Path,
typer.Argument(
exists=True,
file_okay=True,
dir_okay=False,
writable=False,
readable=True,
resolve_path=True,
typer.Option(
"-o",
"--output",
exists=False,
writable=True,
),
],
output: Path,
) -> int:
return pdfly.x2pdf.main(x, output)

Expand Down
37 changes: 26 additions & 11 deletions pdfly/x2pdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Convert a file to PDF."""
"""Convert one or more files to PDF. Each file is a page."""

from pathlib import Path
from typing import List

from fpdf import FPDF
from PIL import Image
Expand All @@ -15,25 +16,39 @@ def px_to_mm(px: float) -> float:
return mm


def image_to_pdf(x: Path, output: Path) -> None:
def image_to_pdf(pdf: FPDF, x: Path) -> None:
cover = Image.open(x)
width: float
height: float
width, height = cover.size
cover.close()
width, height = px_to_mm(width), px_to_mm(height)

pdf = FPDF(unit="mm", format=(width, height))
pdf.add_page()
pdf.add_page(format=(width, height))
pdf.image(x, x=0, y=0)
pdf.output(str(output))


def main(x: Path, output: Path) -> int:
def main(xs: List[Path], output: Path) -> int:
console = Console()
path_str = str(x).lower()
if path_str.endswith(("doc", "docx", "odt")):
console.print("[red]Error: Cannot convert Word documents to PDF")
return -1
image_to_pdf(x, output)
for x in xs:
path_str = str(x).lower()
if path_str.endswith(("doc", "docx", "odt")):
console.print("[red]Error: Cannot convert Word documents to PDF")
return 1
if not x.exists():
console.print(f"[red]Error: File '{x}' does not exist.")
return 2
if output.exists():
console.print(f"[red]Error: Output file '{output}' exist.")
return 3
pdf = FPDF(
unit="mm",
)
for x in xs:
path_str = str(x).lower()
try:
image_to_pdf(pdf, x)
except Exception:
console.print(f"[red]Error: Could not convert '{x}' to a PDF.")
pdf.output(str(output))
return 0
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_x2pdf(tmp_path: Path) -> None:
[
"x2pdf",
"sample-files/003-pdflatex-image/page-0-Im1.jpg",
"--output",
str(output),
],
)
Expand Down

0 comments on commit 6241026

Please sign in to comment.