kiwi0fruit / pyppdf

Pyppeteer PDF. Print html sites and files to pdf via pyppeteer (uses patched pyppeteer that by default downloads updated Chromium revision via https with certifi).
MIT License
37 stars 6 forks source link

Incorporate into Flask Application #2

Open stenlytw opened 5 years ago

stenlytw commented 5 years ago

Hi,

Is there any way to incorporate this module into Flask Application? I tried incorporating into Flask by creating a function:

def render_pdf(string, filename):    
    import pyppdf
    pdf = pyppdf.save_pdf(filename + '.pdf', html=string)   

    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'attachment; filename = ' + filename + '.pdf'
    return response

and call render_template as the first parameter, but got this error:

File "C:\Users\admiin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppdf\pyppeteer_pdf.py", line 216, in save_pdf
return asyncio.get_event_loop().run_until_complete(
File "C:\Users\admiin\AppData\Local\Programs\Python\Python37-32\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-3'.
kiwi0fruit commented 5 years ago

I have no idea. pyppdf module imports asyncio. So may be it's bad to import it inside fuction (but that's just a guess). Few ideas to try:

stenlytw commented 5 years ago

Hi,

Yes, I can import pyppdf outside of the function. Anyway, I found a similar issue in https://stackoverflow.com/q/53679905/3387187, but I still cannot make it work.

kiwi0fruit commented 5 years ago

Have you tried the solution? Both main and save_pdf functions can accept arguments for launch (they should be placed in the launch dictionary field).

kiwi0fruit commented 3 years ago

I still don't know if using main instead of save_pdf solves this issue. Reopening for now.