Using Drupal and DITA OT to Create a PDF File
This post shows you how to use Drupal and DITA OpenToolkit to create a PDF file. It uses Views in Drupal and the REST UI to create a json file that will be used to create a bookmap and markdown files. These files will be used by Open Toolkit to create a PDF file.
Prerequisites: Drupal, DITA OT, Python
Make sure that the following modules are installed in Drupal:
![]() |
Creating Dita Bookmap and Markdown Content Types
- Select Structure > Content Types > Add Content Type from the Administrator menu.
- For the Name, enter: markdown.
- For Description, enter: "Use markdown for creating markdown file."
- Add the fields as shown below. You can Re-use an existing field for Tags.
- Edit the Body field and make sure Body is set to allow Plain text.
- Select Structure > Content Types > Add Content Type from the Administrator menu.
- For the Name enter Dita Bookmap.
- For Description enter: "Use Dita Bookmap for creating a Dita bookmap."
- Add the fields as shown below. You can Re-use an existing field for Tags.
- Edit the Body field and make sure Body is set to allow Plain text.
Creating Markdown Files in Drupal
- Select Content > Add Content > markdown.
- For the Name, enter: Test markdown page 1
- Enter the following information:
- Select Content > Add Content > markdown.
- For the Name, enter: Test markdown page 2
- Enter the following information:
Creating Bookmap File in Drupal
- Select Content > Add Content > Dita Bookmap..
- For the Name, enter: Test Bookmap
- Enter the following information:
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "../dtd/bookmap/dtd/bookmap.dtd"><bookmap id="test1"><booktitle><booklibrary>Test Document</booklibrary><mainbooktitle>Test Document</mainbooktitle><booktitlealt>By: Eric Strausbaugh</booktitlealt></booktitle><frontmatter><booklists><toc/></booklists></frontmatter><chapter href="test1.md" format="markdown"> <topicref href="test_page2.md" format="markdown"></topicref></chapter></bookmap>
Creating a Drupal View and Saving View as a JSON File
The following step shows how to make a view using the dita_test tags that we applied above.
- Select Structure > Views > Add View.
- Enter dita_test for the View Name.
- Select Create Page and use the default settings.
- Click on Provide a Rest Export and chose api/dita_test as the Rest Export Path.
- Click Save and Edit.
- Click on the Edit button for the dita_test view.
- Click on Add under Filter Criteria.
- Click on Tags (field_tags).
- Click Apply.
- Click Apply and Continue.
- For Operator, select Is one of and enter dita_test.
- For Access > Access restrictions: select Role
- For Access > Access options: select Authenticated User
- Click Apply.
- Click on the Rest export display.
- Under Formats, click on Settings.
- Select json as the Accepted request formats and click Apply.
- Under Authentication, choose Basic_Auth
- Click Save.
The Preview should look similar to the following:To save the view as a JSON file:
- Navigate to the view that you created (e.g., http://localhost/api/dita_test). NOTE: You may need to open the view as New Incognito Window.
- Login with your admin login and password.
- In the view, right click and select Save As… (choosing dita_test.json as filename).
Creating Bookmap File and Markdown Files
The following steps will create the bookmap file and markdown files from the dita_test.json file by using a Python script:
- Create a directory (e.g., python_drupal).
- Change directory to the directory that you created.
- Copy the dita_test.json file to this directory.
- Create a file named json_to_dita.py.
- Paste the following contents into the file.
This creates the bookmap and markdown files that you created in previous steps in the out directory.import json import requests import os with open('dita_test.json', 'r') as file: data = json.load(file) length= len(data) print(length) directory_name = "out" # Create the directory try: os.mkdir(directory_name) print(f"Directory '{directory_name}' created successfully.") except FileExistsError: print(f"Directory '{directory_name}' already exists.") except PermissionError: print(f"Permission denied: Unable to create '{directory_name}'.") except Exception as e: print(f"An error occurred: {e}") for x in range(length): if data[x]['field_tags'][0]['target_id'] is not None: file1 = open(directory_name + "/" + data[x]['field_name'][0]['value'], "w") file1.write(data[x]['body'][0]['value']) print(data[x]['vid'][0]['value']) print(data[x]['body'][0]['value']) print(data[x]['field_name'][0]['value']) file1.close()Run python3 json_to_dita.py
Creating PDF File or HTML5 Files
- Within the directory with the bookmap and markdown files, run: ~/dita-ot-x.x/bin/dita --input=test_bookmap.ditamap --format=pdf --output=out
or~/dita-ot-x.x/bin/dita --input=test_bookmap.ditamap --format=html5 --output=outNOTE: DITA-OT must be installed to run the commands above. See https://www.dita-ot.org/dev/topics/installing for installation insuctions.








Comments