Convert various file formats to PDF

Convert EPUB to PDF

Aspose.PDF for PHP allows you simply convert EPUB files to PDF format.

EPUB is a free and open e-book standard from the International Digital Publishing Forum (IDPF). Files have the extension .epub. EPUB is designed for reflowable content, meaning that an EPUB reader can optimize text for a particular display device.

In order to convert EPUB files to PDF format, Aspose.PDF for PHP has a class named EpubLoadOptions which is used to load source EPUB file. After that, the object is passed as an argument to Document object initialization, as it helps the PDF rendering engine to determine the source document’s input format.

The following code snippet shows the process of converting an EPUB file into PDF format.

  1. Create a EPUB LoadOptions.
  2. Initialize Document object.
  3. Save output PDF document.
// Create a new instance of EpubLoadOptions
$loadOption = new EpubLoadOptions();

// Create a new Document object and load the EPUB file
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file
$document->save($outputFile);

Convert Markdown to PDF

Markdown is a text to HTML conversion tool for web authors. Markdown allows you to write in an easy-to-read and write plain text format and then convert it to structurally valid XHTML (or HTML).

The following code snippet shows how to use this functionality with Aspose.PDF for PHP:

// Create a new instance of MdLoadOptions
$loadOption = new MdLoadOptions();

// Create a new instance of Document and load the input Markdown file
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file
$document->save($outputFile);

Convert PCL to PDF

PCL (Printer Command Language) is a Hewlett-Packard printer language developed to access standard printer features. PCL levels 1 through 5e/5c are command-based languages that use control sequences which are processed and interpreted in the order they are received. At a consumer level, PCL data streams are generated by a print driver. PCL output can also be easily generated by custom applications.

Currently, only PCL5 and older versions are supported.

Sets of Commands Support Exceptions Description
Job control commands + Duplex printing mode Control print process: number of copies, output bin, simplex/duplex printing, left and top offsets etc.
Page control commands + Perforation Skip command Specify a size of page, margins, page orientation inter -lines, -character distances etc.
Cursor Positioning Commands +   Specify cursor position and, hence, origins of the text, raster or vector images and details.
Font selection commands +

1. Transparent Print Data Command.

2. Embedded soft fonts. In the current version instead of creating soft font our library selects suitable font from existing “hard” TrueType fonts installed on a target machine.
Suitability is defined by the width/height ratio.
This feature works only for Bitmap and TrueType fonts and does not guarantee that text printed with soft font will be relevant to the one in a source file.
Because character codes in the soft font can unmatched default ones.

3. User-Defined Symbol Sets.

Allow loading soft (embedded) fonts from the PCL file and managing them in memory.
Raster graphics commands + Only black & white Allow loading raster images from PCL file to memory, specify raster parameters
such as width, height, compression type, resolution, etc.
Color commands +   Allow coloring for all printable objects.
Print Model commands +   Allow filling text, raster images and rectangular areas with raster predefined and user-defined patterns, specify transparency mode for patterns and source raster image.
Predefined patterns are hatching, cross-hatch and shading ones.
Rectangle area fill commands +   Allow creation and filling rectangular areas with patterns.
HP-GL/2 Vector Graphics commands + Screened Vector Command (SV), Transparency Mode Command (TR), Transparent Data Command (TD), RO (Rotate Coordinate System), Scalable or Bitmap Fonts Command (SB), Character Slant Command (SL) and Extra Space (ES) are not implemented and DV (Define Variable Text Path) commands are realized in beta version.

- Allow loading HP-GL/2 vector images from the PCL file into memory. Vector image has an origin at the lower-left corner of the printable area, can be scaled, translated, rotated and clipped.

- A vector image can contain text, as labels, and geometric figures such as rectangle, circle, ellipse, line, arc, bezier curve and complex figures composed from the simple ones.

- Closed figures including letters of labels can be filled with solid fill or vector pattern.

- The pattern can be hatching, cross-hatch, shading, raster user-defined, PCL hatching or cross-hatch and PCL user-defined. PCL patterns are raster. Labels can be individually rotated, scaled, and directed in four directions: up, down, left and right. Left and Right directions involve one-after-another letter arrangement. Up and Down directions involve one-under-another letter arrangement.

Macross   Allow loading a sequence of PCL commands into memory and use this sequence many times, for example, to print page header or set one formatting for a set of pages.
Unicode text   Allow printing non-ASCII characters. Not implemented due to lack of sample files with
Unicode text
PCL6 (PCL-XL)   Realized only in the Beta version because of lack in test files. Embedded fonts also are not supported. The JetReady extension is not supported because it is impossible to have JetReady specification. Binary file format.

Converting a PCL file into PDF format

To allow conversion from PCL to PDF, Aspose.PDF for PHP has the class PclLoadOptions which is used to initialize the LoadOptions object. This object is then passed as an argument during Document object initialization and helps the PDF rendering engine to determine the source document’s input format.

The following code snippet shows the process of converting a PCL file into PDF format.

// Create a new instance of PclLoadOptions
$loadOption = new PclLoadOptions();

// Create a new instance of Document and load the PCL file
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file
$document->save($outputFile);

Known Issues

  1. Origin of text strings and images can slightly differ from the ones in a source PCL file if the print direction is not 0º. The same refers to vector images if coordinate system of vector plot is rotated (RO command preceded).
  2. Origin of labels in vector images can be differ from the ones in a source PCL file if the labels are influenced by a sequence of commands: Label Origin (LO), Define Variable Text Path (DV), Absolute Direction (DI) or Relative Direction (DR).
  3. A text can be incorrectly read if it must be rendered with Bitmap or TrueType soft (embedded) font, because currently these fonts are only partially supported (see exceptions in “Supported features table”). In this situation text can be correctly read only if character codes in a soft font corresponds to default ones. A style of the read text also can be differed from the one in source PCL file because it is not necessary to set style in soft font header.
  4. If parsed PCL file contains Intellifont or Universal soft fonts, an exception will be thrown, because Intellifont and Universal font are not supported at all.
  5. If parsed PCL file contains macros commands the result of parsing will strongly differ from the source file, because macros commands are not supported.

Convert Text to PDF

Aspose.PDF for PHP provides the capability to convert Text files to PDF format. In this article, we demonstrate how easily and efficiently we can convert a text file to PDF using Aspose.PDF.

When you need to convert a Text file to PDF, initially read the source text file in some reader. We have used StringBuilder to read the Text file contents. Instantiate Document object and add a new page in the Pages collection. Create a new object of TextFragment and pass StringBuilder object to its constructor. Add a new paragraph in Paragraphs collection using TextFragment object and save the resultant PDF file using the Save method of Document class.

Convert plain text file to PDF

// Create a new Document object.
$document = new Document();

// Add a new page to the document.
$page = $document->getPages()->add();

// Read the content of the input text file.
$text = file_get_contents($inputFile);

// Create a new FontRepository object.
$fontRepository = new FontRepository();

// Find the "Courier" font in the repository.
$font = $fontRepository->findFont("Courier");

// Create a new TextFragment object with the input text.
$textFragment = new TextFragment($text);

// Set the font of the text fragment to "Courier".
$textFragment->getTextState()->setFont($font);

// Add the text fragment to the page.
$page->getParagraphs()->add($textFragment);

// Save the document to the output file.
$document->save($outputFile);

Convert XPS to PDF

Aspose.PDF for PHP support feature converting XPS files to PDF format. Check this article to resolve your tasks.

XPS, XML Paper Specification, is a Microsoft file format used to integrate document creation and viewing into Windows. With Aspose.PDF for PHP, it is possible to convert XPS files to PDF, the portable file format from Adobe.

The file format is basically a zipped XML file, primarily used for distribution and storage. It’s very difficult to edit and mostly implemented by Microsoft.

To convert an XPS file to PDF using Aspose.PDF for PHP, use XpsLoadOptions class. This is used to initialize a LoadOptions object. Later, this object is passed as an argument during the Document object initialization and helps the PDF rendering engine to determine the source document’s input format.

The following code snippet shows the process of converting the XPS file into PDF format.

// Create a new instance of the XpsLoadOptions class
$loadOption = new XpsLoadOptions();

// Create a new instance of the Document class and load the XPS file
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file
$document->save($outputFile);

Convert PostScript to PDF

Aspose.PDF for PHP support features converting PostScript files to PDF format. One of the features from Aspose.PDF is that you can set a set of font folders to be used during conversion.

In order to convert a PostScript file to PDF format, Aspose.PDF for PHP offers PsLoadOptions class which is used to initialize the LoadOptions object. Later this object can be passed as an argument to Document object constructor, which will help PDF Rendering Engine to determine the format of source document.

Following code snippet can be used to convert a PostScript file into PDF format:

// Create a new PsLoadOptions object.
$loadOption = new PsLoadOptions();

// Create a new Document object and load the input PS file.
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file.
$document->save($outputFile);

Convert XML to PDF

The XML format used to store structured data. There are several ways to convert XML to PDF in Aspose.PDF.

Consider option using XML document based on XSL-FO standard.

Convert XSL-FO to PDF

The conversion of XSL-FO files to PDF can be implemented using Document object with XslFoLoadOptions.

// Set the path to the sample files
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFoFile = $dataDir . DIRECTORY_SEPARATOR . "sample.xslt";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.xml";
$outputFile = $dataDir . DIRECTORY_SEPARATOR . "results" . DIRECTORY_SEPARATOR . 'result-xmlfo-to-pdf.pdf';

// Create a new instance of the XslFoLoadOptions class and pass the input XSL-FO file path
$loadOption = new XslFoLoadOptions($inputFoFile);

// Create a new instance of the Document class and pass the input XML file and the XSL-FO load options
$document = new Document($inputFile, $loadOption);

// Save the converted PDF document to the output file path
$document->save($outputFile);

Convert LaTeX/TeX to PDF

The LaTeX file format is a text file format with markup in the LaTeX derivative of the TeX family of languages and LaTeX is a derived format of the TeX system. LaTeX (ˈleɪtɛk/ lay-tek or lah-tek) is a document preparation system and document markup language. It is widely used for the communication and publication of scientific documents in many fields, including mathematics, physics, and computer science. It also has a prominent role in the preparation and publication of books and articles that contain complex multilingual materials, such as Sanskrit and Arabic, including critical editions. LaTeX uses the TeX typesetting program for formatting its output and is itself written in the TeX macro language.

Aspose.PDF for PHP supports the feature to convert TeX files to PDF format and in order to accomplish this requirement, com.aspose.pdf package has a class named LatexLoadOptions which provides the capabilities to load LaTex files and render the output in PDF format using the Document class. The following code snippet shows the process of converting LaTex file to PDF format.

// Create a new instance of the LatexLoadOptions class
$loadOption = new LatexLoadOptions();

// Create a new instance of the Document class and load the TeX file using the TeXLoadOptions
$document = new Document($inputFile, $loadOption);

// Save the document as a PDF file
$document->save($outputFile);