Hướng dẫn xhtml2pdf --css - xhtml2pdf --css
Tôi đang cố gắng cải thiện PDF được tạo của mình được tạo bởi from xhtml2pdf import pisa # import python module # Define your data source_html = "0 của tôi. Show Tôi muốn ví dụ như from xhtml2pdf import pisa # import python module # Define your data source_html = "1, nhưng rõ ràng, nó không hoạt động và tôi không tìm thấy tại sao. Tôi đang đọc rất nhiều tài liệu về thư viện này, tôi đã viết một số tập lệnh CSS nhưng vẫn không có gì. Đây là tệp HTML của tôi:
Bạn có thể thấy nó hoạt động trong thư mục from xhtml2pdf import pisa # import python module # Define your data source_html = "4 Sử dụng trong dòng lệnhXHTML2PDF cũng cung cấp một công cụ dòng lệnh thuận tiện mà bạn có thể sử dụng để chuyển đổi các tệp HTML thành tài liệu PDF bằng dòng lệnh. Lệnh cơ bản này sẽ chuyển đổi nội dung của test.html thành pdf và lưu nó sang test.pdf. Tùy chọn nâng cao sẽ được mô tả sau trong tài liệu này. Tùy chọn from xhtml2pdf import pisa # import python module # Define your data source_html = "5 có thể được sử dụng để khởi động trình xem PDF mặc định sau khi chuyển đổi: Tùy chọn công cụ dòng lệnh nâng caoSử dụng from xhtml2pdf import pisa # import python module # Define your data source_html = "6 để bắt đầu. Chuyển đổi dữ liệu HTMLĐể tạo PDF từ tệp HTML có tên là from xhtml2pdf import pisa # import python module # Define your data source_html = "7 cuộc gọi: PDF kết quả sẽ được gọi là from xhtml2pdf import pisa # import python module # Define your data source_html = "8 (nếu tệp này bị khóa, ví dụ bởi đầu đọc Adobe, nó sẽ được gọi là from xhtml2pdf import pisa # import python module # Define your data source_html = "9, v.v.). Tùy chọn from xhtml2pdf import pisa # import python module # Define your data source_html = "5 chăm sóc PDF sẽ được mở trực tiếp trong Trình xem mặc định hệ điều hành. Để chuyển đổi nhiều tệp, bạn có thể sử dụng các mẫu ký tự đại diện như import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path1 và import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path2: $ xhtml2pdf "test/test-*.html"
Bạn cũng có thể truy cập trực tiếp các trang từ Internet: $ xhtml2pdf -s http://www.xhtml2pdf.com/ Sử dụng các thuộc tính đặc biệtNếu việc chuyển đổi không hoạt động như mong đợi một số thông tin có thể hữu ích.Bạn có thể bật đầu ra của cảnh báo bằng cách thêm import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path3 hoặc thậm chí đầu ra gỡ lỗi bằng cách sử dụng import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path4. Một lý do khác có thể là, việc phân tích cú pháp thất bại.Cân nhắc thử các tùy chọn import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path5 và import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path6. import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path7 sử dụng trình phân tích cú pháp HTMLT5LIB cung cấp hai chế độ phân tích cú pháp bên trong: một cho HTML và một cho XHTML. Khi tạo đầu ra HTML import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path7 sử dụng định nghĩa CSS mặc định bên trong (nếu không tất cả các thẻ sẽ không có sự khác biệt).Để có được ấn tượng về cách thức này trông giống như bắt đầu import os from django.conf import settings from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa from django.contrib.staticfiles import finders def link_callback(uri, rel): """ Convert HTML URIs to absolute system paths so xhtml2pdf can access those resources """ result = finders.find(uri) if result: if not isinstance(result, (list, tuple)): result = [result] result = list(os.path.realpath(path) for path in result) path=result[0] else: sUrl = settings.STATIC_URL # Typically /static/ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/ mUrl = settings.MEDIA_URL # Typically /media/ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/ if uri.startswith(mUrl): path = os.path.join(mRoot, uri.replace(mUrl, "")) elif uri.startswith(sUrl): path = os.path.join(sRoot, uri.replace(sUrl, "")) else: return uri # make sure that file exists if not os.path.isfile(path): raise Exception( 'media URI must start with %s or %s' % (sUrl, mUrl) ) return path7 như thế này: $ xhtml2pdf --css-dump > xhtml2pdf-default.css CSS sẽ được đưa vào tệp def render_pdf_view(request): template_path = 'user_printer.html' context = {'myvar': 'this is your template context'} # Create a Django response object, and specify content_type as pdf response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="report.pdf"' # find the template and render it. template = get_template(template_path) html = template.render(context) # create a pdf pisa_status = pisa.CreatePDF( html, dest=response, link_callback=link_callback) # if error then show some funny view if pisa_status.err: return HttpResponse('We had some errors') return response 0.Bạn có thể sửa đổi điều này hoặc thậm chí lấy một cái hoàn toàn tự xác định và trao nó bằng cách sử dụng tùy chọn'+ html + ' def render_pdf_view(request): template_path = 'user_printer.html' context = {'myvar': 'this is your template context'} # Create a Django response object, and specify content_type as pdf response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="report.pdf"' # find the template and render it. template = get_template(template_path) html = template.render(context) # create a pdf pisa_status = pisa.CreatePDF( html, dest=response, link_callback=link_callback) # if error then show some funny view if pisa_status.err: return HttpResponse('We had some errors') return response 1, ví dụ:'+ html + ' $ xhtml2pdf --css=xhtml2pdf-default.css test.html
|