html2pic 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- html2pic-0.1.1/.gitignore +145 -0
- html2pic-0.1.1/CHANGELOG.md +98 -0
- html2pic-0.1.1/PKG-INFO +347 -0
- html2pic-0.1.1/README.md +314 -0
- html2pic-0.1.1/examples/01_quick_start.py +60 -0
- html2pic-0.1.1/examples/01_quick_start_output.png +0 -0
- html2pic-0.1.1/examples/02_flexbox_card.py +66 -0
- html2pic-0.1.1/examples/02_flexbox_card_output.png +0 -0
- html2pic-0.1.1/examples/03_typography_showcase.py +61 -0
- html2pic-0.1.1/examples/03_typography_showcase_output.png +0 -0
- html2pic-0.1.1/examples/04_shadows_and_effects.py +89 -0
- html2pic-0.1.1/examples/04_shadows_and_effects_output.png +0 -0
- html2pic-0.1.1/examples/05_background_images.py +91 -0
- html2pic-0.1.1/examples/05_background_images_output.png +0 -0
- html2pic-0.1.1/examples/README.md +69 -0
- html2pic-0.1.1/examples/background.webp +0 -0
- html2pic-0.1.1/pyproject.toml +74 -0
- html2pic-0.1.1/src/html2pic/__init__.py +35 -0
- html2pic-0.1.1/src/html2pic/core.py +185 -0
- html2pic-0.1.1/src/html2pic/css_parser.py +290 -0
- html2pic-0.1.1/src/html2pic/exceptions.py +19 -0
- html2pic-0.1.1/src/html2pic/html_parser.py +167 -0
- html2pic-0.1.1/src/html2pic/models.py +168 -0
- html2pic-0.1.1/src/html2pic/style_engine.py +442 -0
- html2pic-0.1.1/src/html2pic/translator.py +944 -0
- html2pic-0.1.1/src/html2pic/warnings_system.py +192 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
2
|
+
__pycache__/
|
3
|
+
*.py[cod]
|
4
|
+
*$py.class
|
5
|
+
|
6
|
+
# C extensions
|
7
|
+
*.so
|
8
|
+
|
9
|
+
# Distribution / packaging
|
10
|
+
.Python
|
11
|
+
build/
|
12
|
+
develop-eggs/
|
13
|
+
dist/
|
14
|
+
downloads/
|
15
|
+
eggs/
|
16
|
+
.eggs/
|
17
|
+
lib/
|
18
|
+
lib64/
|
19
|
+
parts/
|
20
|
+
sdist/
|
21
|
+
var/
|
22
|
+
wheels/
|
23
|
+
pip-wheel-metadata/
|
24
|
+
share/python-wheels/
|
25
|
+
*.egg-info/
|
26
|
+
.installed.cfg
|
27
|
+
*.egg
|
28
|
+
MANIFEST
|
29
|
+
|
30
|
+
# PyInstaller
|
31
|
+
# Usually these files are written by a python script from a template
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33
|
+
*.manifest
|
34
|
+
*.spec
|
35
|
+
|
36
|
+
# Installer logs
|
37
|
+
pip-log.txt
|
38
|
+
pip-delete-this-directory.txt
|
39
|
+
|
40
|
+
# Unit test / coverage reports
|
41
|
+
htmlcov/
|
42
|
+
.tox/
|
43
|
+
.nox/
|
44
|
+
.coverage
|
45
|
+
.coverage.*
|
46
|
+
.cache
|
47
|
+
nosetests.xml
|
48
|
+
coverage.xml
|
49
|
+
*.cover
|
50
|
+
*.py,cover
|
51
|
+
.hypothesis/
|
52
|
+
.pytest_cache/
|
53
|
+
|
54
|
+
# Translations
|
55
|
+
*.mo
|
56
|
+
*.pot
|
57
|
+
|
58
|
+
# Django stuff:
|
59
|
+
*.log
|
60
|
+
local_settings.py
|
61
|
+
db.sqlite3
|
62
|
+
db.sqlite3-journal
|
63
|
+
|
64
|
+
# Flask stuff:
|
65
|
+
instance/
|
66
|
+
.webassets-cache
|
67
|
+
|
68
|
+
# Scrapy stuff:
|
69
|
+
.scrapy
|
70
|
+
|
71
|
+
# Sphinx documentation
|
72
|
+
docs/_build/
|
73
|
+
|
74
|
+
# PyBuilder
|
75
|
+
target/
|
76
|
+
|
77
|
+
# Jupyter Notebook
|
78
|
+
.ipynb_checkpoints
|
79
|
+
|
80
|
+
# IPython
|
81
|
+
profile_default/
|
82
|
+
ipython_config.py
|
83
|
+
|
84
|
+
# pyenv
|
85
|
+
.python-version
|
86
|
+
|
87
|
+
# pipenv
|
88
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
89
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
90
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
91
|
+
# install all needed dependencies.
|
92
|
+
#Pipfile.lock
|
93
|
+
|
94
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
95
|
+
__pypackages__/
|
96
|
+
|
97
|
+
# Celery stuff
|
98
|
+
celerybeat-schedule
|
99
|
+
celerybeat.pid
|
100
|
+
|
101
|
+
# SageMath parsed files
|
102
|
+
*.sage.py
|
103
|
+
|
104
|
+
# Environments
|
105
|
+
.env
|
106
|
+
.venv
|
107
|
+
env/
|
108
|
+
venv/
|
109
|
+
ENV/
|
110
|
+
env.bak/
|
111
|
+
venv.bak/
|
112
|
+
|
113
|
+
# Spyder project settings
|
114
|
+
.spyderproject
|
115
|
+
.spyproject
|
116
|
+
|
117
|
+
# Rope project settings
|
118
|
+
.ropeproject
|
119
|
+
|
120
|
+
# mkdocs documentation
|
121
|
+
/site
|
122
|
+
|
123
|
+
# mypy
|
124
|
+
.mypy_cache/
|
125
|
+
.dmypy.json
|
126
|
+
dmypy.json
|
127
|
+
|
128
|
+
# Pyre type checker
|
129
|
+
.pyre/
|
130
|
+
|
131
|
+
# IDE files
|
132
|
+
.vscode/
|
133
|
+
.idea/
|
134
|
+
*.swp
|
135
|
+
*.swo
|
136
|
+
*~
|
137
|
+
|
138
|
+
# OS generated files
|
139
|
+
.DS_Store
|
140
|
+
.DS_Store?
|
141
|
+
._*
|
142
|
+
.Spotlight-V100
|
143
|
+
.Trashes
|
144
|
+
ehthumbs.db
|
145
|
+
Thumbs.db
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to html2pic will be documented in this file.
|
4
|
+
|
5
|
+
## [0.1.1] - 2024-09-12
|
6
|
+
|
7
|
+
### ✨ New Features
|
8
|
+
- **Linear Gradient Support**: Added full support for CSS `linear-gradient()` in backgrounds
|
9
|
+
- Angle-based gradients: `linear-gradient(135deg, #667eea, #764ba2)`
|
10
|
+
- Keyword directions: `linear-gradient(to right, red, blue)`
|
11
|
+
- Color stops with percentages: `linear-gradient(90deg, red 0%, blue 100%)`
|
12
|
+
- Multiple colors with automatic distribution
|
13
|
+
- Support for all CSS color formats (hex, rgb, rgba, named colors)
|
14
|
+
|
15
|
+
### 🔧 Improvements
|
16
|
+
- Updated documentation with linear-gradient examples and limitations
|
17
|
+
- Enhanced background rendering system to handle both images and gradients
|
18
|
+
- Improved CSS parser to recognize linear-gradient as supported feature
|
19
|
+
|
20
|
+
### 📝 Documentation
|
21
|
+
- Added comprehensive linear-gradient documentation to README
|
22
|
+
- Updated examples to showcase gradient capabilities
|
23
|
+
- Clarified that only linear gradients are supported (radial/conic not yet implemented)
|
24
|
+
|
25
|
+
## [0.1.0] - 2024-09-12
|
26
|
+
|
27
|
+
### Initial Release
|
28
|
+
|
29
|
+
#### ✨ Core Features
|
30
|
+
- **HTML to Image Conversion**: Convert HTML + CSS to high-quality images using PicTex as rendering engine
|
31
|
+
- **Modern CSS Layout**: Full flexbox support with `display: flex`, `justify-content`, `align-items`, `gap`, etc.
|
32
|
+
- **Rich Typography**: Font families, sizes, weights, colors, text decorations, text shadows
|
33
|
+
- **Complete Box Model**: Padding, margins, borders, border-radius support
|
34
|
+
- **Visual Effects**: Box shadows and text shadows with RGBA color support
|
35
|
+
- **Positioning**: Absolute positioning with `left` and `top` properties
|
36
|
+
- **Background Images & Gradients**: Support for `background-image` with `url()` syntax and `linear-gradient()`, plus `background-size` (cover, contain, tile)
|
37
|
+
|
38
|
+
#### 🏗️ Architecture
|
39
|
+
- **Modular Design**: Clean separation of HTML parser, CSS parser, style engine, and translator
|
40
|
+
- **Comprehensive Warnings System**: Detailed debugging information for unsupported features
|
41
|
+
- **CSS Cascade Engine**: Proper specificity calculations and inheritance
|
42
|
+
- **Smart Translation**: Maps HTML/CSS concepts to PicTex builders (Canvas, Row, Column, Text, Image)
|
43
|
+
|
44
|
+
#### 📋 Supported HTML Elements
|
45
|
+
- Container elements: `div`, `section`, `article`, `header`, `footer`, `main`, `nav`, `aside`
|
46
|
+
- Text elements: `h1`-`h6`, `p`, `span`, `strong`, `em`, `b`, `i`, `u`, `s`
|
47
|
+
- Media elements: `img`
|
48
|
+
- List elements: `ul`, `ol`, `li`
|
49
|
+
- Other: `a`, `br`, `hr`
|
50
|
+
|
51
|
+
#### 🎨 Supported CSS Features
|
52
|
+
- **Layout**: `display` (flex, block, inline), `flex-direction`, `justify-content`, `align-items`, `gap`
|
53
|
+
- **Box Model**: `width`, `height`, `padding`, `margin`, `border`, `border-radius`
|
54
|
+
- **Typography**: `font-family`, `font-size`, `font-weight`, `font-style`, `color`, `text-align`, `line-height`, `text-decoration`
|
55
|
+
- **Visual Effects**: `background-color`, `background-image`, `background-size`, `box-shadow`, `text-shadow`
|
56
|
+
- **Positioning**: `position: absolute` with `left` and `top`
|
57
|
+
- **Units**: `px`, `em`, `rem`, `%` support
|
58
|
+
- **Colors**: Hex, RGB, RGBA, and named colors
|
59
|
+
|
60
|
+
#### 🚨 Intelligent Warnings System
|
61
|
+
- **CSS Validation**: Warns about unexpected/invalid CSS property values
|
62
|
+
- **HTML Element Detection**: Warns about unrecognized or unsupported HTML elements
|
63
|
+
- **Feature Limitations**: Clear warnings about unsupported CSS features with alternatives
|
64
|
+
- **Color Fallbacks**: Automatic fallback for invalid colors with warnings
|
65
|
+
- **Categorized Warnings**: Organized by HTML parsing, CSS parsing, style application, etc.
|
66
|
+
|
67
|
+
#### 🔧 Developer Experience
|
68
|
+
- **Simple API**: Clean `Html2Pic(html, css).render()` interface
|
69
|
+
- **Multiple Output Formats**: PNG, JPG via PicTex integration
|
70
|
+
- **Debug Information**: `get_warnings()`, `print_warnings()`, `get_warnings_summary()` methods
|
71
|
+
- **Error Handling**: Graceful degradation with informative warnings instead of crashes
|
72
|
+
|
73
|
+
#### 🏃♂️ Performance Features
|
74
|
+
- **Empty Element Optimization**: Only renders elements with visual styles
|
75
|
+
- **Smart RGBA Parsing**: Proper handling of RGBA colors in shadows and backgrounds
|
76
|
+
- **Efficient Shadow Parsing**: Supports multiple shadows on single elements
|
77
|
+
|
78
|
+
#### 🌟 Special Improvements
|
79
|
+
- **Fixed Empty Div Rendering**: Empty divs with visual styles (background, borders, shadows) now render correctly
|
80
|
+
- **Advanced Shadow Support**: Full CSS shadow syntax with multiple shadows and RGBA colors
|
81
|
+
- **Background Image Integration**: Complete CSS background-image support with PicTex backend
|
82
|
+
- **Comprehensive Property Validation**: Validates CSS values and provides helpful error messages
|
83
|
+
|
84
|
+
### Known Limitations
|
85
|
+
- CSS Grid layout not supported (use Flexbox instead)
|
86
|
+
- Relative positioning not supported (use absolute positioning)
|
87
|
+
- CSS transforms and animations not supported
|
88
|
+
- Background gradients not supported (use solid colors or images)
|
89
|
+
- Complex selectors (descendants, pseudo-classes) not supported
|
90
|
+
|
91
|
+
### Dependencies
|
92
|
+
- PicTex: High-quality rendering engine
|
93
|
+
- BeautifulSoup4: HTML parsing
|
94
|
+
- tinycss2: CSS parsing
|
95
|
+
|
96
|
+
---
|
97
|
+
|
98
|
+
*This project was developed using AI assistance (Claude Code) for rapid prototyping and implementation.*
|
html2pic-0.1.1/PKG-INFO
ADDED
@@ -0,0 +1,347 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: html2pic
|
3
|
+
Version: 0.1.1
|
4
|
+
Summary: Convert HTML + CSS to images without a browser
|
5
|
+
Project-URL: Homepage, https://github.com/francozanardi/html2pic
|
6
|
+
Project-URL: Repository, https://github.com/francozanardi/html2pic.git
|
7
|
+
Project-URL: Bug Tracker, https://github.com/francozanardi/html2pic/issues
|
8
|
+
Project-URL: Documentation, https://github.com/francozanardi/html2pic/blob/main/README.md
|
9
|
+
Project-URL: Examples, https://github.com/francozanardi/html2pic/tree/main/examples
|
10
|
+
Author-email: Franco Zanardi <francozanardi97@gmail.com>
|
11
|
+
License-Expression: MIT
|
12
|
+
Keywords: conversion,css,html,image,rendering
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
14
|
+
Classifier: Intended Audience :: Developers
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
16
|
+
Classifier: Operating System :: OS Independent
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
23
|
+
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
25
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
26
|
+
Requires-Python: >=3.8
|
27
|
+
Requires-Dist: beautifulsoup4>=4.9.0
|
28
|
+
Requires-Dist: pictex
|
29
|
+
Requires-Dist: tinycss2>=1.1.0
|
30
|
+
Provides-Extra: examples
|
31
|
+
Requires-Dist: pillow>=8.0.0; extra == 'examples'
|
32
|
+
Description-Content-Type: text/markdown
|
33
|
+
|
34
|
+
# html2pic
|
35
|
+
|
36
|
+
**Convert HTML + CSS to beautiful images using PicTex**
|
37
|
+
|
38
|
+
`html2pic` is a Python library that translates a subset of HTML and CSS to high-quality images without requiring a browser engine. Built on top of [PicTex](https://github.com/francozanardi/pictex), it provides a powerful and intuitive way to generate images from web markup.
|
39
|
+
|
40
|
+
## ⚠️ **Experimental Software Notice**
|
41
|
+
|
42
|
+
**This is experimental software developed primarily using AI assistance (Claude Code).** While functional, it should be used with caution in production environments. Key considerations:
|
43
|
+
|
44
|
+
- **Rapid Development**: Most of the codebase was generated and refined through AI-assisted programming
|
45
|
+
- **Limited Testing**: Extensive testing in diverse environments is still ongoing
|
46
|
+
- **Active Development**: APIs and behavior may change significantly in future versions
|
47
|
+
- **Community Feedback Welcome**: Please report issues and suggest improvements via GitHub
|
48
|
+
|
49
|
+
Use this library for prototyping, experimentation, and non-critical applications. For production use, thorough testing is recommended.
|
50
|
+
|
51
|
+
## 🚀 Key Features
|
52
|
+
|
53
|
+
- **No Browser Required**: Pure Python implementation using PicTex as the rendering engine
|
54
|
+
- **Flexbox Support**: Modern CSS layout with `display: flex`, `justify-content`, `align-items`, etc.
|
55
|
+
- **Rich Typography**: Font families, sizes, weights, colors, and text decorations
|
56
|
+
- **Box Model**: Complete support for padding, margins, borders, and border-radius
|
57
|
+
- **Responsive Sizing**: Supports px, em, rem, % units and flexible sizing modes
|
58
|
+
- **High Quality Output**: Vector (SVG) and raster (PNG, JPG) output formats
|
59
|
+
- **Simple API**: Clean, intuitive interface inspired by modern web development
|
60
|
+
|
61
|
+
## 📦 Installation
|
62
|
+
|
63
|
+
```bash
|
64
|
+
pip install html2pic
|
65
|
+
```
|
66
|
+
|
67
|
+
## 🎯 Quick Start
|
68
|
+
|
69
|
+
```python
|
70
|
+
from html2pic import Html2Pic
|
71
|
+
|
72
|
+
# Your HTML content
|
73
|
+
html = '''
|
74
|
+
<div class="card">
|
75
|
+
<h1>Hello, World!</h1>
|
76
|
+
<p class="subtitle">Generated with html2pic</p>
|
77
|
+
</div>
|
78
|
+
'''
|
79
|
+
|
80
|
+
# Your CSS styles
|
81
|
+
css = '''
|
82
|
+
.card {
|
83
|
+
display: flex;
|
84
|
+
flex-direction: column;
|
85
|
+
align-items: center;
|
86
|
+
padding: 30px;
|
87
|
+
background-color: #f0f8ff;
|
88
|
+
border-radius: 15px;
|
89
|
+
width: 300px;
|
90
|
+
}
|
91
|
+
|
92
|
+
h1 {
|
93
|
+
color: #2c3e50;
|
94
|
+
font-size: 28px;
|
95
|
+
margin: 0 0 10px 0;
|
96
|
+
}
|
97
|
+
|
98
|
+
.subtitle {
|
99
|
+
color: #7f8c8d;
|
100
|
+
font-size: 16px;
|
101
|
+
margin: 0;
|
102
|
+
}
|
103
|
+
'''
|
104
|
+
|
105
|
+
# Create and render
|
106
|
+
renderer = Html2Pic(html, css)
|
107
|
+
image = renderer.render()
|
108
|
+
image.save("output.png")
|
109
|
+
```
|
110
|
+
|
111
|
+
## 🏗️ Architecture
|
112
|
+
|
113
|
+
html2pic works by translating HTML + CSS concepts to PicTex builders:
|
114
|
+
|
115
|
+
1. **HTML Parser**: Uses BeautifulSoup to create a DOM tree
|
116
|
+
2. **CSS Parser**: Uses tinycss2 to extract style rules
|
117
|
+
3. **Style Engine**: Applies CSS cascade, specificity, and inheritance
|
118
|
+
4. **Translator**: Maps styled DOM nodes to PicTex builders (Canvas, Row, Column, Text, Image)
|
119
|
+
5. **Renderer**: Uses PicTex to generate the final image
|
120
|
+
|
121
|
+
## 📋 Supported HTML/CSS Features
|
122
|
+
|
123
|
+
### HTML Elements
|
124
|
+
- `<div>`, `<section>`, `<article>` → Layout containers
|
125
|
+
- `<h1>`-`<h6>`, `<p>`, `<span>` → Text elements
|
126
|
+
- `<img>` → Image elements
|
127
|
+
|
128
|
+
### CSS Layout
|
129
|
+
- `display: flex` with `flex-direction: row|column`
|
130
|
+
- `justify-content`: `flex-start`, `center`, `flex-end`, `space-between`, `space-around`, `space-evenly`
|
131
|
+
- `align-items`: `flex-start`, `center`, `flex-end`, `stretch`
|
132
|
+
- `gap` for spacing between flex items
|
133
|
+
|
134
|
+
### CSS Box Model
|
135
|
+
- `width`, `height` (px, %, auto, fit-content, fill-available)
|
136
|
+
- `padding`, `margin` (shorthand and individual sides)
|
137
|
+
- `border` with width, style (solid, dashed, dotted), and color
|
138
|
+
- `border-radius` (px and %)
|
139
|
+
- `background-color` (solid colors and linear gradients)
|
140
|
+
- `background-image` (url() and linear-gradient())
|
141
|
+
- `background-size` (cover, contain, tile for images)
|
142
|
+
- `box-shadow` (offset-x, offset-y, blur-radius, color)
|
143
|
+
|
144
|
+
### CSS Typography
|
145
|
+
- `font-family`, `font-size`, `font-weight`, `font-style`
|
146
|
+
- `color`, `text-align`, `line-height`
|
147
|
+
- `text-decoration` (underline, line-through)
|
148
|
+
- `text-shadow` (offset-x, offset-y, blur-radius, color)
|
149
|
+
|
150
|
+
#### Font Loading
|
151
|
+
html2pic supports loading fonts from the system or custom font files:
|
152
|
+
|
153
|
+
```css
|
154
|
+
/* System font */
|
155
|
+
font-family: "Arial", sans-serif;
|
156
|
+
|
157
|
+
/* Custom font file (provide absolute path) */
|
158
|
+
font-family: "/path/to/custom-font.ttf";
|
159
|
+
|
160
|
+
/* Multiple fallbacks */
|
161
|
+
font-family: "Custom Font", "Arial", sans-serif;
|
162
|
+
```
|
163
|
+
|
164
|
+
**Note**: When using custom fonts, provide the full path to the font file (.ttf, .otf). If the font cannot be loaded, it will fall back to system defaults.
|
165
|
+
|
166
|
+
#### Linear Gradients
|
167
|
+
html2pic supports CSS linear-gradient syntax for backgrounds:
|
168
|
+
|
169
|
+
```css
|
170
|
+
/* Angle-based gradients */
|
171
|
+
background-image: linear-gradient(135deg, #667eea, #764ba2);
|
172
|
+
|
173
|
+
/* Direction keywords */
|
174
|
+
background-image: linear-gradient(to right, red, blue);
|
175
|
+
|
176
|
+
/* Color stops with percentages */
|
177
|
+
background-image: linear-gradient(90deg, #ff0000 0%, #00ff00 50%, #0000ff 100%);
|
178
|
+
|
179
|
+
/* Multiple colors */
|
180
|
+
background-image: linear-gradient(45deg, yellow, orange, red, purple);
|
181
|
+
```
|
182
|
+
|
183
|
+
**Supported features:**
|
184
|
+
- Angle directions (0deg, 45deg, 135deg, etc.)
|
185
|
+
- Keyword directions (to right, to bottom, to top left, etc.)
|
186
|
+
- Color stops with percentages
|
187
|
+
- Multiple colors with automatic distribution
|
188
|
+
- All CSS color formats (hex, rgb, rgba, named colors)
|
189
|
+
|
190
|
+
**Limitations**: Only linear gradients are supported. Radial and conic gradients are not yet implemented.
|
191
|
+
|
192
|
+
### CSS Positioning
|
193
|
+
- `position: absolute` with `left` and `top` properties (px, %, em, rem)
|
194
|
+
- **Limitation**: Only `left` and `top` are supported, not `right` or `bottom`
|
195
|
+
|
196
|
+
### CSS Selectors
|
197
|
+
- Tag selectors: `div`, `p`, `h1`
|
198
|
+
- Class selectors: `.my-class`
|
199
|
+
- ID selectors: `#my-id`
|
200
|
+
- Universal selector: `*`
|
201
|
+
|
202
|
+
## 📝 Examples
|
203
|
+
|
204
|
+
Explore the `examples/` folder for complete runnable examples with generated output images.
|
205
|
+
|
206
|
+
### Quick Start Example
|
207
|
+
Basic card layout demonstrating core html2pic functionality:
|
208
|
+
|
209
|
+
```python
|
210
|
+
from html2pic import Html2Pic
|
211
|
+
|
212
|
+
html = '''
|
213
|
+
<div class="card">
|
214
|
+
<h1>Hello, World!</h1>
|
215
|
+
<p class="subtitle">Generated with html2pic</p>
|
216
|
+
</div>
|
217
|
+
'''
|
218
|
+
|
219
|
+
css = '''
|
220
|
+
.card {
|
221
|
+
display: flex;
|
222
|
+
flex-direction: column;
|
223
|
+
align-items: center;
|
224
|
+
padding: 30px;
|
225
|
+
background-color: #f0f8ff;
|
226
|
+
border-radius: 15px;
|
227
|
+
width: 300px;
|
228
|
+
}
|
229
|
+
|
230
|
+
h1 {
|
231
|
+
color: #2c3e50;
|
232
|
+
font-size: 28px;
|
233
|
+
margin: 0 0 10px 0;
|
234
|
+
}
|
235
|
+
|
236
|
+
.subtitle {
|
237
|
+
color: #7f8c8d;
|
238
|
+
font-size: 16px;
|
239
|
+
margin: 0;
|
240
|
+
}
|
241
|
+
'''
|
242
|
+
|
243
|
+
renderer = Html2Pic(html, css)
|
244
|
+
image = renderer.render()
|
245
|
+
image.save("output.png")
|
246
|
+
```
|
247
|
+
|
248
|
+

|
249
|
+
|
250
|
+
### Flexbox Card Layout
|
251
|
+
Social media style user card with horizontal layout:
|
252
|
+
|
253
|
+
```python
|
254
|
+
html = '''
|
255
|
+
<div class="user-card">
|
256
|
+
<div class="avatar"></div>
|
257
|
+
<div class="user-info">
|
258
|
+
<h2>Alex Doe</h2>
|
259
|
+
<p>@alexdoe</p>
|
260
|
+
</div>
|
261
|
+
</div>
|
262
|
+
'''
|
263
|
+
|
264
|
+
css = '''
|
265
|
+
.user-card {
|
266
|
+
display: flex;
|
267
|
+
align-items: center;
|
268
|
+
gap: 15px;
|
269
|
+
padding: 20px;
|
270
|
+
background-color: white;
|
271
|
+
border-radius: 12px;
|
272
|
+
border: 1px solid #e1e8ed;
|
273
|
+
}
|
274
|
+
|
275
|
+
.avatar {
|
276
|
+
width: 60px;
|
277
|
+
height: 60px;
|
278
|
+
background-color: #1da1f2;
|
279
|
+
border-radius: 50%;
|
280
|
+
}
|
281
|
+
|
282
|
+
.user-info h2 {
|
283
|
+
margin: 0 0 4px 0;
|
284
|
+
font-size: 18px;
|
285
|
+
color: #14171a;
|
286
|
+
}
|
287
|
+
|
288
|
+
.user-info p {
|
289
|
+
margin: 0;
|
290
|
+
color: #657786;
|
291
|
+
font-size: 14px;
|
292
|
+
}
|
293
|
+
'''
|
294
|
+
```
|
295
|
+
|
296
|
+

|
297
|
+
|
298
|
+
### Advanced Visual Effects
|
299
|
+
Shadows, positioning, and advanced styling features:
|
300
|
+
|
301
|
+

|
302
|
+
|
303
|
+
### Background Images
|
304
|
+
Background image support with different sizing modes:
|
305
|
+
|
306
|
+

|
307
|
+
|
308
|
+
**Complete examples** with full source code are available in the [`examples/`](examples/) directory.
|
309
|
+
|
310
|
+
## 🔧 API Reference
|
311
|
+
|
312
|
+
### Html2Pic Class
|
313
|
+
|
314
|
+
```python
|
315
|
+
Html2Pic(html: str, css: str = "", base_font_size: int = 16)
|
316
|
+
```
|
317
|
+
|
318
|
+
**Methods:**
|
319
|
+
- `render(crop_mode=CropMode.SMART) -> BitmapImage`: Render to raster image
|
320
|
+
- `render_as_svg(embed_font=True) -> VectorImage`: Render to SVG
|
321
|
+
- `debug_info() -> dict`: Get debugging information about parsing and styling
|
322
|
+
|
323
|
+
### Output Objects
|
324
|
+
|
325
|
+
The rendered images are PicTex `BitmapImage` or `VectorImage` objects with methods like:
|
326
|
+
- `save(filename)`: Save to file
|
327
|
+
- `to_numpy()`: Convert to NumPy array
|
328
|
+
- `to_pillow()`: Convert to PIL Image
|
329
|
+
- `show()`: Display the image
|
330
|
+
|
331
|
+
## 🚧 Current Limitations
|
332
|
+
|
333
|
+
This is an early version focusing on core functionality. A lot of features are not yet supported.
|
334
|
+
|
335
|
+
## 🤝 Contributing
|
336
|
+
|
337
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
338
|
+
|
339
|
+
## 📄 License
|
340
|
+
|
341
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
342
|
+
|
343
|
+
## 🙏 Acknowledgments
|
344
|
+
|
345
|
+
- Built on top of [PicTex](https://github.com/your-pictex-repo) for high-quality rendering
|
346
|
+
- Uses [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/) for HTML parsing
|
347
|
+
- Uses [tinycss2](https://github.com/Kozea/tinycss2) for CSS parsing
|