Giter VIP home page Giter VIP logo

markdown2pdf's Introduction

Markdown2Pdf

A simple library to convert markdown to pdf, using Java

##Why Sometimes, it might come in handy to create a pdf version of a markdown file. This library combines several other libraries to leverage the hassle of converting everything by yourself.

The codebase is kept fairly small, while still providing a fluent API.

##Prerequisites ###Version 1.0.x This library only relies on the fact that you have at least Java JDK 1.6 installed. We support Java 1.8, and as of version 1.1 (which is still being worked on) we will make great use of the new features Java 8 has to offer.

<dependency>
	<groupId>eu.de-swaef.pdf</groupId>
	<artifactId>Markdown2Pdf</artifactId>
	<version>2.0.1</version>
</dependency>

###Version 1.1.x and up This version of the library will only support Java 1.8.

##Building > git clone https://github.com/Qkyrie/Markdown2Pdf.git > mvn clean install

##API examples Instead of just providing you with examples here, we created an entire Class dedicated on teaching you the basics of the API. The class can be found in the repository, but for the current version, basically, this is it:

/**
 * Welcome to the tutorial for Markdown2Pdf.
 * We tried to create a page to demonstrate how to use our API. But in the end, we found that
 * it was a lot easier to see how to use it when effectively using our code in a working example.
 * <p/>
 * If you look at the examples listed below, you should get the hang of how you'll be able to use
 * and integrate this library in one of your own projects
 */
public class Tutorial {

    @Test
    public void basicExample() throws Markdown2PdfLogicException, ConversionException {

        /*
            Let's create an instance of the markdown2PdfConverter to work with
         */
        Markdown2PdfConverter markdown2PdfConverter =
                Markdown2PdfConverter.newConverter();

        /*
            We'll have to specify where our converter has to get his markdown data.
            With Java 8 in mind, we chose for a more generic approach, where we only have to
            implement a class defined by Markdown2PdfReader
         */
        markdown2PdfConverter.readFrom(() -> "***Test***");

        /*
            Same thing goes for our writer. When the reading, cleaning and converting is done,
            we need to do something with the resulted bytes. What you do with it is entirely up to you.
         */
        markdown2PdfConverter.writeTo(out -> {
            //do something with it here
        });

        /*
            What has the API done at this point? The simple answer: nothing. It only set the states and placeholders
            for what it will be doing. It's lazily executed, which means that you'll have to explicitly make it do the
            work in order for it to read, clean and convert.

            Actually making the API do the work can be done as follows:
         */
        markdown2PdfConverter.doIt();
    }

    /**
     * Our API has been implemented in such a way, that you can use it as a oneliner.
     * The above example can be rewritten, as shown in the this example
     */
    @Test
    public void basicExampleAsOneLiner() throws Markdown2PdfLogicException, ConversionException {
        Markdown2PdfConverter
                .newConverter()
                .readFrom(() -> "***Test***")
                .writeTo(out -> {
                    //here you can just do something with the bytes, like write it to a file
                    //for example.
                })
                .doIt();
    }

    /**
     * It might seem tedious, implementing two interfaces in order to make the execution work, but believe us when
     * we say that it's for the best. With Java 8 in mind, these interfaces will be functional interfaces, and will be
     * replaceable with Lambda expressions.
     *
     * Fortunately, we provided some Simple implementations which can serve as replacement
     * for your own implementation of the interfaces. Just follow the
     */
    @Test
    public void additionalUtilityClasses() {
       utilityReaders();
       utilityWriters();
    }

    /**
     * UtilityReaders are simple implementations of the
     * Markdown2PdfReader interface
     */
    private void utilityReaders() {
        simpleStringMarkdown2PdfReader();
    }

    /**
     * UtilityWriters on the other hand are simple implementation
     * of the Markdown2PdfWriter interface
     */
    private void utilityWriters() {
        simpleFileMarkdown2PdfWriter();
    }

    /**
     * the SimpleStringMarkdown2PdfReader can be used to read from a String. It's the most simple
     * of implementations, which you'll probably find using a lot.
     */
    private void simpleStringMarkdown2PdfReader() {
        Markdown2PdfConverter
                .newConverter()
                .readFrom(new SimpleStringMarkdown2PdfReader("***Test***"));
    }

    /**
     * The SimpleFileMarkdown2PdfWriter implements the Markdown2PdfWriter in such a way, that the only
     * thing you need to provide is a file. It will write the resulted bytes to the file.
     */
    private void simpleFileMarkdown2PdfWriter() {
        Markdown2PdfConverter
                .newConverter()
                .writeTo(new SimpleFileMarkdown2PdfWriter(new File("pathname_comes_here")));
    }

}

markdown2pdf's People

Contributors

qkyrie avatar raphapy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

markdown2pdf's Issues

Java 6 support

Could you please add support for Java 6. It's impossible to use this library in "Enterprise" projects (most of them are written on Java 6).

License question

Hi!
First of all, thank you for the great library!
Can you consider licencing under LGPL instead of AGPL so that we could use it in our project? It looks like this would be fine since flying-saucer-pdf uses LGPL as well.

Chinese words will turn to be garbled after converting

Recently, I want to develop a tool to convert document with markdown syntax. so, I use your library to help me to archive it. Unfortunately, the garbled code makes me confused. If the library supports to set the charset , maybe I will solve it by myself!

JTidy breaks unicode characters

Hey, not sure if this is still being developed but here it goes.

JTidy dependency used to convert HTML snippet to full page will malformat special characters (þ, æ, ð, á etc.) as it's bugged and uses UTF-16

Temporary hacky solution is to replace HtmlCleaner#clean with

return String.format("<html><head><meta charset=\"UTF-8\" /></head><body>%s</body></html>", input);

Incorrect rendered "A" variations

Accented letter A is not being rendered properly:

Trying to render:
&Aacute;&Eacute;&Iacute;&Oacute;&Uacute; &aacute;&eacute;&iacute;&oacute;&uacute;

Should get: ÁÉÍÓÚ áéíóú
Actual Result: &Aacute;ÉÍÓÚ &aacute;éíóú

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.