Xbasic

HTML_to_PDF Function

Syntax

HTML_to_PDF()

Arguments

htmlFile_or_textCharacter

The path to an HTML file or a character string that contains HTML to convert to PDF.

pdf_filenameCharacter

Default = "". The PDF filename. If not specified, a filename will be generated for you.

obj_inPointer

Default = null_value(). If no pdf_filename is specified, you can pass an object object, obj_in, which will be populated with the generated filename for the PDF. It is recommended to pass in an object if you do not specify a pdf filename.

Returns

resultLogical

Returns .t. if the operation succeeded. Otherwise, .f..

Description

Converts an HTML file to a PDF file.

Discussion

The HTML_TO_PDF() function converts an HTML file (or an HTML string), with any links to contents in the same folder, into a PDF file.

dim htmlFile as c = "c:/test/myHtml.html"
dim htmlText as c = file.to_string("c:/test/myHtml2.html")
dim pdfFilename as c = "c:/test/myHtml.pdf"
dim obj as p

' Generated PDF from file
? HTML_TO_PDF(htmlFile,pdfFilename)
= .T.

' Generating a PDF from text without specifying a filename
? HTML_TO_PDF(htmlText,'',obj)
= .T.

? obj.pdfFilename
= "C:\Users\Janet\AppData\Local\Temp\AlphaAnywhere\p_29384\a5_temp_a5a14599_7b14_4fd4_a3fb_74e49378b006.pdf"

Specifying the Paper Size 

You can specify page size (height and width) and margins (top, bottom, left, right) for the PDF directly in the HTML. The page size is defined as a JSON string in an HTML comment. For example:

<!--PAPER:
    {
    "width": "11in",
    "height": "8.5in",
    "margin": {
        "left": "0.5in",
        "right": "0.5in",
        "top": "1in",
        "bottom": "1in"
    }
}
-->

The PDF generated using the paper size definition above will have a page width of 11 inches and height of 8.5 inches (landscape) with left and right margins of 0.5 inches and top and bottom margins of 1 inch.