Exporting scripts as a PDF

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Itadori_yujiii

Hello,
Is it possible to export all my scripts as PDF or word document without having to copy and paste each script?

Why would you want to do this. Word/PDF are terrible formats for code. Code is plain text.

kidscancode | 2021-03-20 15:39

Its because I need it for documentation, for my A-Level coursework

Itadori_yujiii | 2021-03-20 15:48

Well, copy & paste is going to be your solution, then. This is not something that any programmer ever needs, so it’s not going to be built in to the engine.

If you want syntax highlighted code, you can use a web-based code formatter (there are a bunch you can search for - Github does it too if you’re keeping your code there) and when you c&p into word it will keep the HTML formatting.

kidscancode | 2021-03-20 15:56

Why would you want to do this. Word/PDF are terrible formats for code. Code is plain text.

I was thinking the exact same thing

Wakatta | 2021-03-20 18:04

thank you this was helpful :slight_smile:

Itadori_yujiii | 2021-03-20 18:36

You know what’s really helpful … my answer

Wakatta | 2021-03-21 18:04

:bust_in_silhouette: Reply From: Wakatta

Go to Project > Project Settings > Plugins Tab
Create a new plugin and add the following to the script file

tool
extends EditorPlugin

var button = Button.new()
var editor_file_dialog = EditorFileDialog.new()

var property_info = {
	"name": "filesystem/chrome/chrome_exec",
	"type": TYPE_STRING,
	"hint": PROPERTY_HINT_FILE,
	"hint_string": "*chrome.exe"
}

func _on_Button_pressed():
	var scriptEditor = get_editor_interface().get_script_editor()
	var script = scriptEditor.get_current_script()
	if script:
		var chrome
		if (not ProjectSettings.has_setting("filesystem/chrome/chrome_exec") or 
		not ProjectSettings.get_setting("filesystem/chrome/chrome_exec")):
			editor_file_dialog.window_title = "Choose chrome file"
			#scriptEditor.get_node("editor_file_dialog").show()
			editor_file_dialog.show()
			editor_file_dialog.invalidate()
			return
		chrome = ProjectSettings.get_setting("filesystem/chrome/chrome_exec")

		var source_file = ProjectSettings.globalize_path(script.resource_path)
		var file_name
		var options = ["--headless", "--disable-gpu", "--print-to-pdf="]
		var output_file = source_file.replace(".gd", ".pdf")
		var print_options = "--no-margins"

		if not File.new().file_exists(chrome):
			print("Chome missing cannot do conversion")

		var everything : PoolStringArray
		everything.append_array(options)
		everything[-1] += output_file
		everything.append(print_options)
		everything.append(source_file)

		if OS.execute(chrome, everything, false):
			if File.new().file_exists(output_file):
				print(output_file)
			
func file_selected(file_name):
	ProjectSettings.set("filesystem/chrome/chrome_exec", file_name)
	ProjectSettings.add_property_info(property_info)
	_on_Button_pressed()

func _enter_tree():
	button.text = "Convert to Pdf"
	
	button.connect("button_up", self, "_on_Button_pressed")
	var scriptEditor = get_editor_interface().get_script_editor()
	var script_panel = scriptEditor.get_child(0).get_child(0)
	var debug = script_panel.get_child(4)
	
	editor_file_dialog.set_mode(EditorFileDialog.MODE_OPEN_FILE)
	editor_file_dialog.add_filter("chrome")
	editor_file_dialog.set_current_dir("c:/Program Files")
	editor_file_dialog.set_current_path("c:")
	if OS.get_name() == "X11":
		editor_file_dialog.set_current_dir("/opt/")
		editor_file_dialog.set_current_path("/opt/")
	editor_file_dialog.set_access(EditorFileDialog.ACCESS_FILESYSTEM)
	editor_file_dialog.rect_position = Vector2(200, 200)
	editor_file_dialog.rect_size = Vector2(800, 600)
	editor_file_dialog.name = "editor_file_dialog"
	
	if scriptEditor.has_node("editor_file_dialog"):
		editor_file_dialog = scriptEditor.get_node("editor_file_dialog")
	else:
		scriptEditor.add_child(editor_file_dialog)
		script_panel.add_child_below_node(debug, button)
	
	editor_file_dialog.connect("file_selected", self, "file_selected")
	if OS.get_name() == "X11":
		property_info.hint_string = "chrome"

func _exit_tree():
	pass
	
func get_plugin_name():
	return "GD2PDF"

The way its works is to use chrome to do the conversion so if you don’t have chrome then you must be an alien who doesn’t know what this (1e100) means

While on the script workspace press convert and it will make a pdf of the active script in the same directory with the same name.

Once again i say

This is not something that any programmer ever needs to do

So not tested for all use cases and i’m pretty sure i’m breaking all kinds a rules
Also if you wanted to do batch converting you’re on you own but its pretty easy to loop through the project filesystem to achieve it