README.md: add more examples of how to use the library
This commit is contained in:
parent
e1b2adf70a
commit
44ce296581
1 changed files with 29 additions and 3 deletions
32
README.md
32
README.md
|
@ -135,7 +135,33 @@ You can then test the converter using:
|
||||||
The package can also be used as a library:
|
The package can also be used as a library:
|
||||||
|
|
||||||
import img2pdf
|
import img2pdf
|
||||||
pdf_bytes = img2pdf.convert('test.jpg')
|
|
||||||
|
|
||||||
file = open("name.pdf","wb")
|
# opening from filename
|
||||||
file.write(pdf_bytes)
|
with open("name.pdf","wb") as f:
|
||||||
|
f.write(img2pdf.convert('test.jpg'))
|
||||||
|
|
||||||
|
# opening from file handle
|
||||||
|
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
|
||||||
|
f1.write(img2pdf.convert(f2))
|
||||||
|
|
||||||
|
# using in-memory image data
|
||||||
|
with open("name.pdf","wb") as f:
|
||||||
|
f.write(img2pdf.convert("\x89PNG...")
|
||||||
|
|
||||||
|
# multiple inputs (variant 1)
|
||||||
|
with open("name.pdf","wb") as f:
|
||||||
|
f.write(img2pdf.convert("test1.jpg", "test2.png"))
|
||||||
|
|
||||||
|
# multiple inputs (variant 2)
|
||||||
|
with open("name.pdf","wb") as f:
|
||||||
|
f.write(img2pdf.convert(["test1.jpg", "test2.png"]))
|
||||||
|
|
||||||
|
# writing to file descriptor
|
||||||
|
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
|
||||||
|
img2pdf.convert(f2, outputstream=f1)
|
||||||
|
|
||||||
|
# specify paper size (A4)
|
||||||
|
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
|
||||||
|
layout_fun = img2pdf.get_layout_fun(a4inpt)
|
||||||
|
with open("name.pdf","wb") as f:
|
||||||
|
f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))
|
||||||
|
|
Loading…
Reference in a new issue