TL;DR
AnyDoc is a versatile framework that treats document generation as an HTML/CSS code generation task, making the outputs fully editable and structurally robust. By combining a massive new synthetic dataset (DocHTML) with a novel Height-Aware Reinforcement Learning (HARL) technique, AnyDoc outperforms state-of-the-art models across text-to-document, derendering, and element-layout tasks while successfully eliminating content overflow.
What Problem Does This Paper Address?
Automated document generation—creating flyers, presentations, menus, or resumes from design intents or raw elements—is a challenging AI task. Existing approaches typically suffer from a fundamental trade-off in how they represent documents:
- Raster Images: Diffusion-based models generate visually stunning results, but the output is a flat image, making it completely uneditable.
- Flat Coordinate Sequences: Models predict bounding boxes (JSON style) for elements. While editable, this approach forces the AI to do complex pixel-level math, often leading to overlapping text, messy alignment, and broken structural hierarchies.
Furthermore, high-quality document datasets are scarce and expensive to manually curate. Finally, standard language model training lacks a mechanism to ensure that text actually fits within the document's physical boundaries, frequently causing ugly text truncation and content overflow.
Methods
AnyDoc shifts the paradigm by generating documents natively in HTML/CSS. This leverages decades of web layout engineering (like Flexbox and Grid) so the model can focus on high-level design intent rather than micro-managing pixel coordinates.
The framework operates in three main stages:
- Automated Data Synthesis Pipeline: To solve data scarcity, the authors used a large-scale document repository, used an MLLM (InternVL3) to extract semantic annotations, and leveraged a code generation model (Qwen3-Coder-480B) to synthesize structured HTML/CSS templates. Image placeholders were filled using FLUX.1-dev, and the final pages were rendered via Playwright.
- Supervised Fine-Tuning (SFT): The authors fine-tuned a multi-modal LLM (Qwen2.5-VL-7B-Instruct) on their synthetic dataset using LoRA to handle three distinct tasks: Intention-to-Document (I2D), Document Derendering (DD), and Element-to-Document (E2D).
- Height-Aware Reinforcement Learning (HARL): To fix content overflow, the authors introduced a post-training RL stage based on Group Relative Policy Optimization (GRPO). During training, the model's generated code is rendered in real-time using a headless browser (Playwright). A custom reward function penalizes the model if the actual rendered height deviates from the target document height.
Datasets
The paper introduces DocHTML, a large-scale, multi-category synthetic dataset.
- Scale: 265,206 document samples.
- Diversity: Spans 111 distinct document categories and 32 unique design styles.
- Contents: Each sample includes rich metadata (design intentions, textual descriptions), clean HTML/CSS source code, high-quality visual assets, and rendered screenshots.
Key Insights and Ideas
- Outsourcing Math to the Browser: LLMs are notoriously poor at precise spatial geometry. By forcing the model to output modern CSS layout primitives (like
row-gap,justify-content, andflex-basis), AnyDoc lets the browser's rendering engine do the heavy lifting of calculating exact positions. - Closing the Visual Loop with RL: Standard language model training only optimizes for token probability, completely blind to how the text looks visually. By integrating Playwright directly into the reinforcement learning loop, the model receives a direct physical reward signal based on whether the text actually fits on the page.
Key Results
AnyDoc was evaluated against general-purpose MLLMs (GPT-4o, InternVL3) and specialized baselines (OpenCOLE, WebSight, Design2Code, LaDeCo).
- Intention-to-Document (I2D): AnyDoc achieved a Layout score of 8.64 and an Image score of 8.92, outperforming GPT-4o (8.59 / 8.75) and specialized text-to-image models like FLUX.1-dev.
- Document Derendering (DD): AnyDoc excelled at converting screenshots back into clean code, achieving a Position score of 0.910 and a Text score of 0.986, significantly beating dedicated screenshot-to-code models like Design2Code.
- Overflow Mitigation: Ablation studies showed that adding the HARL post-training phase dropped the content overflow metric from 0.392 down to 0.085, proving the effectiveness of the height-aware reward function.
Limitations and Future Work
While AnyDoc achieves remarkable layout fidelity and full editability, the authors note a few areas for improvement:
- Model Scale Constraints: Because AnyDoc uses a relatively compact 7B base model, it occasionally experiences minor overflow issues compared to massive frontier models like GPT-4o. The authors believe scaling up the base model will naturally alleviate this.
- Scope Expansion: Future work will focus on extending the framework to support additional document tasks, such as document variation (generating alternatives of an existing layout) and document-based question answering.
Summary updated