我正在尝试创建一个epaper Web应用程序,管理员可以在其中上传pdf格式的文件,并在从客户端渲染页面时将其转换为图像。我已经尝试了pdf2image,但是却遇到了类似这样的错误:
PDFPageCountError at /city_list
Unable to get page count.
I/O Error: Couldn't open file '['/media/pdf/Akola/2020/04/24/page01.pdf']': No such file or directory.
这是我的代码:
models.py
class City(models.Model):
cityid =models.AutoField(primary_key=True)
name = models.CharField( max_length=50)
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
ab = instance.date.strftime("%Y/%m/%d")
return 'pdf/{0}/{1}/{2}'.format(instance.city.name,ab, filename)
class DailyFeed(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE)
date =models.DateField(auto_now=False, auto_now_add=False)
page1 = models.FileField(upload_to=user_directory_path)
page2 = models.FileField(upload_to=user_directory_path)
page3 = models.FileField(upload_to=user_directory_path)
views.py:
def city(request):
city_list = City.objects.all()
daily = DailyFeed.objects.filter(date=timezone.now().date()).filter(city__name = "Akola")
ab = [pdfurl.page1.url for pdfurl in daily ]
path = r'{}'.format(ab)
a = str(path)
pages = convert_from_path('{}'.format(a), 200)
for pdf in pages:
pdf.save('{}.jpg'.format(a.name[:-4]), 'JPEG')
return render (request,'main.html',{'city_list':city_list,'page':pdf})
urls.py
re_path(r'^city_list',views.city,name='city'),
我没弄错我要去哪里?
答案 0 :(得分:0)
使用路径代替url。 URL不是文件所在的位置,而是浏览器可以访问它的方式
[pdfurl.page1.url for pdfurl in daily ]
到
[pdfurl.page1.path for pdfurl in daily ]