How to put Word documents online so they can be downloaded, printed and read via a web page? [closed]

The real problem here is the download offer. Let’s separate the two tasks first, to see that better.

Upload

1. … from Word directly

Word can communicate with WordPress directly per xmlrpc.php. When you ope a new file, select Blog post as template, then enter your user credentials once. Word will remember those. It might be useful to use a separate account for that, just in case MS is phoning home that too.

Now you can publish your Word text directly to WordPress. You can also edit them later, and even import existing blog posts for further editing. Keep in mind that Word will embed its own styles directly into the HTML code. This is rather ugly.

There is a post on WPMU Dev about the details.

2. … as PDF

There are many drivers for Windows which allow you to print a document as PDF. Either find one with a developer API and an event handling, so you can attach an upload handler to the print action, or write a custom shell script that is watching one directory on your hard drive and will upload the file whenever one is changed or replaced.

The tricky part here is that you cannot publish a PDF as blog post, and you have to add the file to the media library. The latter can be done per XML RPC too.

Download

If your text is stored as HTML, like in the first option, you would have to convert the HTML back to Word again. There are many tools for that. You could try PHPWord.

The actual WordPress handler could listen on the actions in wp-admin/admin-post.php as described in Export data as CSV in back end with proper HTTP headers:

if ( is_admin() )
{
    $action = 'print_doc';
    add_action( "admin_post_nopriv_{$action}", 'print_word' );
    add_action( "admin_post_{$action}", 'print_word' );
}

function print_word()
{
    $post_id = filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT );

    if ( ! $post_id )
        return;

    $post = get_post( $post_id );

    status_header( 200 );
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Disposition: attachment; filename=" . $post->post_name . ".docx');
    header('Pragma: no-cache');

    // Convert $post->post_content to Word
    // Output the Word document
    exit;
}

To create the link on the front end, you can filter the_content:

if ( ! is_admin() )
{
    add_filter( 'the_content', function( $content ) {

        if ( ! is_singular() )
            return $content;

        $post_id = get_the_ID();
        $url = admin_url( 'admin-post.php' );

        $link = sprintf(
            '<p class="download-link"><a href="https://wordpress.stackexchange.com/questions/241878/%s">Download</a></p>',
            admin_url( 'admin-post.php' ) . '?action=print_doc&amp;id=' . $post_id
        );

        return $content . $link;
    });
}

If you have uploaded the file as PDF, use get_attached_media() here, filter its return value for PDF. and create the link the same way as above.