<?php
require_once ('../../../../wp-load.php');
require_once ('../includes/utils.inc.php');

$post_id = $_POST['post_id'];
$template = $_POST['template'];

$post = get_post( $post_id );
$post_type = $post->post_type;
$post_type_object = get_post_type_object( $post_type );
if ( !current_user_can($post_type_object->cap->edit_post, $post_id) ) {
	wp_die( __('You are not allowed to edit this item.') );
}

global $base;

delete_post_meta($post_id, 'pripre_entry_style');
add_post_meta($post_id, 'pripre_entry_style', $template);

$dir = pripre_get_base_dir();
$base = "$dir/templates/book/$template";
$copper = pripre_get_copper();

header("Content-Type: application/pdf");
//header("Content-Type: text/plain");

include ("../includes/copper-common.inc.php");
$template = pripre_get_template($template);
$copper->property("output.page-width", $template['width']."mm");
$copper->property("output.page-height", $template['height']."mm");

$copper->start_resource("style.css", array('encoding' => 'UTF-8'));
include ("$base/style.css");
$copper->end_resource();

$copper->start_main(".", array('encoding' => 'UTF-8'));
$title =  str_replace('\\"', '"', $_POST['title']);
$content =  str_replace('\\"', '"', $_POST['content']);
$contents_template = "$base/entry.php";
include ("$dir/templates/html.php");
$copper->end_main();
$copper->close();
?>
