求支持@page指令与命名页面的HTML转PDF API(兼容PHP)
Hey Grega, totally get the struggle—tracking down APIs that handle advanced CSS Paged Media features like @page directives and named pages (for per-page headers/footers) isn’t easy, especially when you need solid PHP compatibility. Here are some solid alternatives to DocRaptor/Prince to check out:
WeasyPrint
This open-source tool has full support for CSS Paged Media, including named@pagerules and custom headers/footers. While it’s built with Python, integrating it with PHP is straightforward: you can either call its command-line interface viaexec()/shell_exec()in PHP, or use Composer packages that wrap it for easier integration. It’s great if you want a self-hosted option or can use a third-party hosted WeasyPrint API.PDFreactor
A commercial, enterprise-grade solution that fully supports the CSS Paged Media specification—including named pages,@pagedirectives, and granular control over page-specific headers/footers. They offer an official PHP SDK, so integrating it into your PHP project is seamless. Like Prince, it’s optimized for high-quality, print-ready PDFs, making it a direct competitor to DocRaptor.Puppeteer (via PHP Wrappers)
Puppeteer (the headless Chrome/Chromium tool) has decent support for@pagerules, and while named pages might require a bit of creative styling/JavaScript workarounds, you can absolutely implement per-page headers and footers. For PHP, libraries likespatie/browsershotwrap Puppeteer, letting you generate PDFs directly from your PHP code. It’s a great choice if your content is more web-focused and you want to leverage Chrome’s rendering engine.wkhtmltopdf (with Custom Workarounds)
Out-of-the-box, wkhtmltopdf has limited support for advanced@pagefeatures, but many developers use JavaScript and CSS hacks to mimic named page behavior and per-page headers/footers. With PHP libraries likeknplabs/knp-snappy, you can easily integrate wkhtmltopdf into your project. It’s a budget-friendly option, though keep in mind the workarounds might be less robust than native support.
Quick Tips:
- Always test each tool with your specific CSS to ensure it handles your named pages and header/footer logic correctly—implementation details can vary across engines.
- For self-hosted needs, WeasyPrint or wkhtmltopdf are solid picks; for managed services, PDFreactor offers hosted API plans similar to DocRaptor.
- If strict compliance with CSS Paged Media is critical, PDFreactor and WeasyPrint are your best bets over Puppeteer/wkhtmltopdf.
内容的提问来源于stack exchange,提问作者greentech

