0

I have a Django project which generates a PDF file. This below is the corresponding code:

def createPDF(name):

    current_path = os.path.dirname(os.path.dirname(__file__))

    template = get_template(f'src.tex')
    context = {
         'content': name,
    }

    rendered_tpl = template.render(context).encode('utf-8')

    process = subprocess.Popen(
        ['pdflatex', '-output-directory', f'{current_path}/templates'],
        stdin=PIPE,
        stdout=PIPE,
    )

    process.communicate(rendered_tpl)
    Path(f'{current_path}/templates/texput.pdf').rename(f'{current_path}/media/pdf/myfirstcv.pdf')

When I run the local server and run my function the PDF is saved in my templates directory. However, after deploying on Heroku, and generating the PDF the PDF is not found. I tried to look for it in the bash, but it is just not there.

What is wrong?

This is the error message:

[Errno 2] No such file or directory: '/app/templates/texput.pdf' -> '/app/media/pdf/myfirstcv.pdf'

There are multiple possibilities:

  • The PDF is not created at all (even if it works on localserver)
    • Is there a way to check if the file is created on Heroku?
  • The PDF is not named texput.pdf (even if the name is always the same on the localserver)
    • I assume the name is going to be always the same.
  • The PDF is not saved into templates folder as expected
    • Is there a way to check if a file was saved somewhere else?
GCMeccariello
  • 339
  • 2
  • 13
  • What makes you think the file is not there? Have you tried opening it again from your code? (Note that if you `heroku run bash` you will _not_ end up on the same dyno where your code is running. There could be any number of those. You'll end up on a one-off dyno, with its own separate filesystem.) – ChrisGPT was on strike Jul 03 '22 at 21:29
  • I think it is not there because after generating it, I want to change the location. I did it with: `Path(f'{current_path}/templates/texput.pdf').rename(f'{current_path}/media/pdf/myfirstcv.pdf')`. This command calls an error. Note: on the local server it works. – GCMeccariello Jul 03 '22 at 21:32
  • Please always share _exact_ error messages. "An error" isn't very helpful. Why would this file be called `texput.pdf`? I don't see that anywhere in the code you have shared above. – ChrisGPT was on strike Jul 03 '22 at 21:34
  • To be honest, I do not know why the file is called texput.pdf. it just always generates this name. The code just stops when trying to change the location of the file. – GCMeccariello Jul 03 '22 at 21:37
  • "The code just stops" and "this command calls an error" say two different things. Which is it? Please [edit] this code and any error messages into the question. – ChrisGPT was on strike Jul 03 '22 at 21:38
  • @Chris : Do you mind checking my question again? Would highly appreciate it. – GCMeccariello Jul 03 '22 at 22:30
  • There is a reason why you can't use [SQLite](https://devcenter.heroku.com/articles/sqlite3#disk-backed-storage) as database on Heroku. It's due to how their [ephemeral filesystem](https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem) works. Basically, you can't programmatically generate files in the platform. – Christopher Tabula Jul 04 '22 at 01:39
  • Does the `/app/media/pdf/` directory exist? – ChrisGPT was on strike Jul 04 '22 at 14:43
  • @ChristopherTabula, there's nothing wrong with using Heroku's local filesystem as scratch space. It is possible to write to it and read from it. The problem is when those modifications are expected to survive dyno restarts, which they do not. – ChrisGPT was on strike Jul 04 '22 at 14:43

1 Answers1

0

The last time I checked, you can create or generate files on the Heroku machine but they will disappear after you make a new request (i.e: if you visit a new page).

DISCLAIMER: This is just my understanding based on observation and testing.

I'd suggest that you set up a storage area such as Cloudinary, AWS, Firebase, etc. to store the file before trying to retrieve it.

Damoiskii
  • 1,328
  • 1
  • 5
  • 20
  • "you can create or generate files on the Heroku machine but they will disappear after you make a new `request` (_i.e: if you visit a new page_)"—citation needed. Yes, each dyno's ephemeral filesystem gets reset whenever the dyno restarts, but this definitely doesn't happen every request. If OP is just treating the local filesystem as temporary scratch space they should be fine to do so. – ChrisGPT was on strike Jul 04 '22 at 18:13
  • (Also, how would that even work? Web servers are generally designed to handle many requests at the same time. Which new request would cause the files to be removed? And which files, from which earlier requests would be cleared up that way?) – ChrisGPT was on strike Jul 04 '22 at 18:15
  • @Damoiskii, I have set up my storage system. My files are generated on my local machine, however, I just can not save it "programmatically" to AWS S3. If I upload a file through the admin panel it works.. any advise? https://stackoverflow.com/questions/73373188/django-generate-file-and-save-it-to-model – GCMeccariello Aug 16 '22 at 14:05