Skip to content

Commit

Permalink
feat: Use optimize_images option instead of optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
ronisaha committed Feb 18, 2024
1 parent 69c1aa7 commit 6b128cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ POST /api/v1.0/print

#### Parameters

| Parameter | Type | Required | Description |
|:--------------|:-----------------|:------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `html` | `file or string` | __Semi-Required__ | HTML file to convert. html or url or report one is required. Only either url or html, report should be used. |
| `url` | `file or string` | __Semi-Required__ | URL to convert. html or url or report one is required. Only either url or html, report should be used. |
| `report` | `string` | __Semi-Required__ | Report template name to render the html. html or url or report one is required. Only either url or html, report should be used. |
| `data` | `dict` | __Semi-Required__ | Variables as dictionary for rendering report template. Used along with `report` parameter. |
| `optimize` | `string` | __Optional__ | Optimize size of generated PDF. Can contain Coma seperated "images" and "fonts". |
| `disposition` | `string` | __Optional__ | Set response `disposition` type(attachment or inline). default is inline. |
| `file_name` | `string` | __Optional__ | Set response `disposition file_name`. default is `document.pdf`. |
| `password` | `string` | __Optional__ | Password protected PDF |
| `template` | `string` | __Optional__ | Template name for the use of predefined templates. |
| `driver` | `string` | __Optional__ | `wk\|weasy(default)` wk=`wkhtnltopdf` weasy=`Weasyprint` |
| `options` | `json` | __Optional__ | Only with driver=`wk` all supported `wkhtmltopdf` options are supported |
| `style` | `file or string` | __Optional__ | Style to apply to the `html`. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as `asset`. Only either `style` or `style[]` can be used. |
| `style[]` | `file or file[]` | __Optional__ | Multiple styles to apply to the `html`. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as `asset`. Only either `style` or `style[]` can be used. |
| `asset[]` | `file or file[]` | __Optional__ | Assets which are referenced in the html. This can be images, CSS or fonts. The name must be 1:1 the same as used in the files. |
| Parameter | Type | Required | Description |
|:------------------|:-----------------|:------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `html` | `file or string` | __Semi-Required__ | HTML file to convert. html or url or report one is required. Only either url or html, report should be used. |
| `url` | `file or string` | __Semi-Required__ | URL to convert. html or url or report one is required. Only either url or html, report should be used. |
| `report` | `string` | __Semi-Required__ | Report template name to render the html. html or url or report one is required. Only either url or html, report should be used. |
| `data` | `dict` | __Semi-Required__ | Variables as dictionary for rendering report template. Used along with `report` parameter. |
| `optimize_images` | `boolean` | __Optional__ | Whether size of embedded images should be optimized, with no quality loss. |
| `disposition` | `string` | __Optional__ | Set response `disposition` type(attachment or inline). default is inline. |
| `file_name` | `string` | __Optional__ | Set response `disposition file_name`. default is `document.pdf`. |
| `password` | `string` | __Optional__ | Password protected PDF |
| `template` | `string` | __Optional__ | Template name for the use of predefined templates. |
| `driver` | `string` | __Optional__ | `wk\|weasy(default)` wk=`wkhtnltopdf` weasy=`Weasyprint` |
| `options` | `json` | __Optional__ | Only with driver=`wk` all supported `wkhtmltopdf` options are supported |
| `style` | `file or string` | __Optional__ | Style to apply to the `html`. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as `asset`. Only either `style` or `style[]` can be used. |
| `style[]` | `file or file[]` | __Optional__ | Multiple styles to apply to the `html`. This should only be used if the CSS is not referenced in the html. If it is included via HTML link, it should be passed as `asset`. Only either `style` or `style[]` can be used. |
| `asset[]` | `file or file[]` | __Optional__ | Assets which are referenced in the html. This can be images, CSS or fonts. The name must be 1:1 the same as used in the files. |


#### Response
Expand Down
8 changes: 4 additions & 4 deletions weasyprint_rest/print/weasyprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def __init__(self, html=None, url=None, template=None):
self.url = url
self.template = template if template is not None else Template()

def write(self, optimize_size, password=None, driver=None, options=None):
def write(self, optimize_images=False, password=None, driver=None, options=None):

if driver == 'wk':
pdf_bytes = self._write_with_pdfkit(options)
else:
pdf_bytes = self._write_with_weasyprint(optimize_size)
pdf_bytes = self._write_with_weasyprint(optimize_images)

if password is None:
return pdf_bytes
Expand Down Expand Up @@ -96,7 +96,7 @@ def _prepare_base_dir(self):

return base_dir

def _write_with_weasyprint(self, optimize_size):
def _write_with_weasyprint(self, optimize_images):
if self.url is not None:
html = HTML(url=self.url, encoding="utf-8", url_fetcher=self.template.url_fetcher)
else:
Expand All @@ -105,7 +105,7 @@ def _write_with_weasyprint(self, optimize_size):
font_config = self.template.get_font_config()
styles = self.template.get_styles() if self.template is not None else []
pdf_bytes = html.write_pdf(stylesheets=styles, image_cache=None, font_config=font_config,
optimize_images=True)
optimize_images=optimize_images)
return pdf_bytes

def _cleanup_dir(self, base_dir, html_file):
Expand Down
9 changes: 2 additions & 7 deletions weasyprint_rest/web/rest/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def post(self):
driver = _parse_request_argument("driver", 'weasy')
url = _parse_request_argument("url", None)
report = _parse_request_argument("report", None)
optimize = _parse_request_argument("optimize", None)
optimize_images = _parse_request_argument("optimize_images", False)

if driver not in['weasy', 'wk']:
return abort(422, description="Invalid value for driver! only wk or weasy supported")
Expand Down Expand Up @@ -114,16 +114,11 @@ def post(self):

password = _parse_request_argument("password", None)

if optimize is not None:
optimize_size = tuple(optimize.split(","))
else:
optimize_size = ()

options = None
if driver == 'wk':
options = json.loads(_parse_request_argument("options", '{}'))

content = printer.write(optimize_size, password=password, driver=driver, options=options)
content = printer.write(optimize_images, password=password, driver=driver, options=options)

# build response
response = make_response(content)
Expand Down

0 comments on commit 6b128cb

Please sign in to comment.