How it works...

Either set up a model administration and enter details for a CV with some Experience entries there, or do so via the Django shell. Then, if you access the document's URL, such as at http://127.0.0.1:8000/cv/1/pdf/, you will be asked to download a PDF document that looks something similar to the following:

How does the view work? First, we load a curriculum vitae by its ID (as given in the URL) if it exists, or we raise a page-not-found error if it doesn't. Then, we create the response object with the content type for a PDF document. We set the Content-Disposition header to attachment with a filename based on the string representation for the CV. This will force the browsers to open a download prompt asking the user to save the PDF document and suggesting the specified name for the file.

For the filename here, we are using the built-in Django utility function to slugify the CurriculumVitae instance (represented by the first and last name). Learn more about slugs in the Using HTML5 data attributes recipe from Chapter 4Templates and JavaScript.

Next, we render the HTML template as a string, passing the curriculum vitae object into the context. For the static smiley image, we load the static template tag library, and use the corresponding {% static %} tag to output its URL. Similarly, we expose MEDIA_URL via the {% get_media_prefix %} tag from the same library, though it isn't used in the current template.

The resulting html string is passed to the xhtml2pdf PDF creation method. This method also takes a link_callback function, which is responsible for ensuring the sources used for images, backgrounds, or style sheets can be found by xhtml2pdf for inclusion in the PDF. The result is a status object indicating whether or not an error occurred. If one has, we respond with a simple error message and server error response, rather than the successful PDF response.

Let's take a look at the HTML template that is used to create this document. The template has some unusual markup tags and CSS rules. If we want to have some elements on each page of the document, we can create what are called frames for that. In the preceding example, the <div> tag with the footerContent ID is marked as a frame, which will be repeated at the bottom of each page. In a similar way, we can have a header or background image for each page. More complex layouts are also possible.

The following are the specific markup tags used in this document:

  • The <pdf:nextpage> tag sets a manual page break
  • The <pdf:pagenumber> tag returns the number of the current page
  • The <pdf:pagecount> tag returns the total number of pages

The current version 0.2.3 of the Pisa xhtml2pdf library doesn't fully support all HTML tags and CSS rules. http://xhtml2pdf.readthedocs.io/en/latest/reference.html provides a listing of some things that are known to be supported, but there are no publicly accessible benchmarks to see what exactly is supported and at what level. Therefore, you would need to experiment in order to match a PDF document to design requirements. However, this library is still mighty enough for customized layouts, which can be created primarily with only knowledge of HTML and CSS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.38.125