# Download PDFs

## Basic Usage

```bash
axm-bib pdf 10.48550/arXiv.1706.03762
```

Downloads the PDF, extracts content to Markdown, saves figures as PNG, and writes `paper.bib` — all in one command.

### Output Structure

```
~/axm/papers/vaswani2017attention/
├── vaswani2017attention.pdf   # downloaded PDF
├── paper.bib                  # BibTeX entry
├── content.md                 # extracted Markdown
└── figures/
    ├── fig_001.png
    └── ...
```

```
Downloaded: ~/axm/papers/vaswani2017attention
  PDF: vaswani2017attention.pdf (1,234,567 bytes)
  BibTeX: paper.bib
  Markdown: content.md (45,123 chars, 12 pages)
  Figures: 8
```

## Email Configuration

The Unpaywall API requires an email address. On first use, `axm-bib` will prompt you:

```
Unpaywall requires an email. Enter yours: user@example.com
Saved to ~/.config/axm-bib/config.toml
```

Alternatively, set it via environment variable:

```bash
export UNPAYWALL_EMAIL="user@example.com"
```

Resolution order:

1. `UNPAYWALL_EMAIL` environment variable
2. `~/.config/axm-bib/config.toml`
3. Interactive prompt (saved for next time)

## How Fallback Works

```mermaid
graph TD
    A[Unpaywall API] --> B{OA locations?}
    B -->|Yes| C[Try repository URLs first]
    C -->|403 error| D[Try next URL]
    D -->|All failed| E[Raise error]
    C -->|Success| F["Save to {key}/ folder"]
    B -->|No| G{arXiv DOI?}
    G -->|Yes| H["Direct: arxiv.org/pdf/{id}"]
    H --> F
    G -->|No| I[Not open access]
    F --> J[Extract Markdown + figures]
    J --> K[Write paper.bib]
```

URLs are tried in priority order:

1. **Repositories** (arXiv, PubMed Central) — most reliable
2. **Publishers** — may return 403
3. **arXiv fallback** — direct URL for `10.48550/arXiv.*` DOIs

## Standalone Extraction

Already have a PDF? Use `extract` directly:

```bash
axm-bib extract paper.pdf -o ~/output/
```

This creates `content.md` + `figures/` without downloading.

## Not Open Access?

If the paper has no OA version and isn't on arXiv:

```
Paper is not open access — no PDF available.
```

## Python API

```python
from axm_bib.pipeline import BibPipeline

pipeline = BibPipeline()
result = pipeline.download_pdf("10.48550/arXiv.1706.03762")

if result:
    print(f"Folder: {result.paper_dir}")
    print(f"PDF:    {result.path} ({result.size_bytes} bytes)")
    print(f"BibTeX: {result.bibtex_path}")
    if result.extraction:
        ext = result.extraction
        print(f"Markdown: {ext.markdown_path} ({ext.char_count} chars)")
        print(f"Figures:  {len(ext.figures)}")
else:
    print("Not open access")
```
