Skip to content

Commit

Permalink
example usage file
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerkumar18 committed May 29, 2019
1 parent 3275bd9 commit 0d165ca
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ You can pass the `api_key`, `api_secret` and `workspace` explicitly. Alternative
>>> pdf_client = PDFGenerator(api_key='<PDF_GENERATOR_KEY>', api_secret='<PDF_GENERATOR_SECRET>')
>>> pdf_client.set_workspace('<PDF_GENERATOR_WORKSPACE>')
```

##### Generate a new Document
```python
>>> new_pdf = pdf_client.create_document(template_id=48484, data={"name": "Sameer Kumar"}, document_format="pdf", response_format="url")
>>> new_pdf.response
'https://us1.pdfgeneratorapi.com/share/5434/ce2fc41de8e51fc7db2cbc1700075a92'
```
##### Fetch All Templates
```python
>>> templates = pdf_client.all_templates(tags=['test_tag'], access=['private'])
Expand All @@ -64,12 +71,6 @@ You can pass the `api_key`, `api_secret` and `workspace` explicitly. Alternative
>>> template.layout.format
'A4'
```
##### Generate a new Document
```python
>>> new_pdf = pdf_client.create_document(template_id=48484, data={"name": "Sameer Kumar"}, document_format="pdf", response_format="url")
>>> new_pdf.response
'https://us1.pdfgeneratorapi.com/share/5434/ce2fc41de8e51fc7db2cbc1700075a92'
```
##### Create a new template
```python
>>> new_template = pdf_client.create_template(name='<TEMPLATE_NAME>')
Expand Down Expand Up @@ -103,14 +104,6 @@ Run the test with the following command:
$ python setup.py test
```

## Documentation

Please see [https://docs.pdfgeneratorapi.com/](https://docs.pdfgeneratorapi.com/) for complete up-to-date documentation.

## About PDFGeneratorAPI.com

PDF Generator API is a RESTful API and a template builder for creating PDF documents from Your software, from Your data. With PDF Generator API you can allow your users to create and manage different document templates with an easy-to-use browser based document editor. And you can merge templates with data from your own software via RESTful API to generate PDF and HTML documents.

## Default Values

You can explicitly override certain default assumptions like -
Expand All @@ -124,7 +117,19 @@ You can explicitly override certain default assumptions like -
- Document Format: `document_format`. Default - "pdf"
- Response Format: `response_format`. Default - "base64"

### About Author
## Documentation

Please see [https://docs.pdfgeneratorapi.com/](https://docs.pdfgeneratorapi.com/) for complete up-to-date documentation.

## About PDFGeneratorAPI.com

PDF Generator API is a RESTful API and a template builder for creating PDF documents from Your software, from Your data. With PDF Generator API you can allow your users to create and manage different document templates with an easy-to-use browser based document editor. And you can merge templates with data from your own software via RESTful API to generate PDF and HTML documents.

## Support
If you have any API related query/issue please contact support@pdfgeneratorapi.com

For any wrapper related query/issue, please raise a GitHub issue.
## About Author
[Sameer Kumar](https://www.sameerkumar.website/)

Find me on [Twitter](https://twitter.com/sameer_kumar018)
50 changes: 50 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Sample code snippets you can refer for easy copy/paste

from pdfgeneratorapi import PDFGenerator

pdf_client = PDFGenerator(
api_key="61e5f04ca1794253ed17e6bb986c1702",
api_secret="68db1902ad1bb26d34b3f597488b9b28",
)
pdf_client.set_workspace("demo.example@actualreports.com")


# For defaults of document_format, response_format, access and other API defaults, please check the `Default Values` section of Readme
# Generate a new Document
new_pdf = pdf_client.create_document(
template_id=48484,
data={"name": "Sameer Kumar"},
document_format="pdf",
response_format="url",
)

# Fetch All Templates
all_templates = pdf_client.all_templates()
print(all_templates)

# Advanced optional fields =>
# Fetch all templates and filter them by a tag
all_templates = pdf_client.all_templates(tags=["test_tag"])
print(all_templates)

# Fetch all templates and filter them by a access type
all_templates = pdf_client.all_templates(access=["private"])


# Get template info by ID
template = pdf_client.get_template(template_id=48484)

# Create a new template
new_template = pdf_client.create_template(name="some new template")


# Duplicate an existing template
# `name` is optional. You can create a template copy without specifying the name
copy_template = pdf_client.create_template_copy(
template_id=48484, name="Copied template"
)


# Get a one-click link to web editor
# Just forms a url
editor_url = pdf_client.get_editor_url(template_id=48484, data={"name": "Sameer"})
9 changes: 7 additions & 2 deletions pdfgeneratorapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


def create_py_object(item: dict):
if not len(item):
return item
map_item = PDFGeneratorResponse(item)

map_item.__setattr__("to_dict", item)
Expand All @@ -35,5 +37,8 @@ def dict_to_object(data: dict):
map_item = create_py_object(element)
response_list.append(map_item)
return response_list
response_object = create_py_object(response_dict)
return response_object
if len(response_dict):
response_object = create_py_object(response_dict)
return response_object
else:
return []
2 changes: 1 addition & 1 deletion pdfgeneratorapi/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def create_template(self, name):
return response

@make_response
def create_template_copy(self, template_id: int, name: str):
def create_template_copy(self, template_id: int, name: str = ""):
""" Creates a copy of a template to the workspace.
:param template_id: Unique ID of the template.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

"""
pdfgeneratorapi
~~~~~~~
~~~~~~~~~~~~~~~
Setup for PDFGeneratorAPI
"""

Expand Down
6 changes: 3 additions & 3 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PDF_GENERATOR_KEY="6bbfbd37fce6ed418fb1ba2ee38b0be2a89745cab507b53d3befb8e29493234e"
PDF_GENERATOR_SECRET="7af96b0e40ee232a7946c7f45aabe1d13d470cf123d06f25ba1b5f5e5a2b58b7"
PDF_GENERATOR_WORKSPACE="sam@sameerkumar.website"
PDF_GENERATOR_KEY="61e5f04ca1794253ed17e6bb986c1702"
PDF_GENERATOR_SECRET="68db1902ad1bb26d34b3f597488b9b28"
PDF_GENERATOR_WORKSPACE="demo.example@actualreports.com"

0 comments on commit 0d165ca

Please sign in to comment.