Skip to content

Instantly share code, notes, and snippets.

@dantetesta
Last active March 27, 2024 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantetesta/4c5854f81b5c095191ba1e3864656598 to your computer and use it in GitHub Desktop.
Save dantetesta/4c5854f81b5c095191ba1e3864656598 to your computer and use it in GitHub Desktop.
WordPress Shortcode - ConvertAPI WEB TO PDF - HTML to PDF - Dante Testa
<?php
/* Shortcode --->>> [botao_pdf] */
function registrar_shortcode_botao_pdf() {
add_shortcode('botao_pdf', 'renderizar_botao_pdf');
add_action('wp_enqueue_scripts', 'adicionar_scripts_ajax');
}
add_action('init', 'registrar_shortcode_botao_pdf');
function renderizar_botao_pdf() {
// Adiciona o CSS para o botão e o spinner
$botao_pdf_css = "<style>
#converter-pdf {
padding: 10px 15px;
background-color: #0073aa;
color: #ffffff;
border: none;
border-radius: 3px;
font-size: 16px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
transition: background-color 0.3s ease;
}
#converter-pdf:hover {
background-color: #0096ee;
}
#converter-pdf:disabled {
background-color: #00a0d2;
cursor: not-allowed;
}
#converter-pdf .fa,
#converter-pdf .spinner {
margin-right: 5px;
}
#converter-pdf .spinner {
border: 2px solid #f3f3f3; /* Light grey */
border-top: 2px solid #3498db; /* Blue */
border-radius: 50%;
width: 14px;
height: 14px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>";
echo $botao_pdf_css . '<button id="converter-pdf"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> Download&nbsp;<span id="spinner" class="spinner" style="display:none;"></span></button>';
}
function adicionar_scripts_ajax() {
wp_enqueue_script('jquery');
wp_localize_script('jquery', 'ajaxobject', array('ajaxurl' => admin_url('admin-ajax.php')));
wp_add_inline_script('jquery', "
jQuery(document).ready(function($) {
var botaoPDF = $('#converter-pdf');
var spinner = $('#spinner');
botaoPDF.click(function() {
botaoPDF.prop('disabled', true);
spinner.show();
$.ajax({
url: ajaxobject.ajaxurl,
type: 'POST',
data: {
action: 'converter_para_pdf',
urlPagina: window.location.href
},
success: function(response) {
var data = JSON.parse(response);
if(data && data.Files && data.Files.length > 0) {
var fileUrl = data.Files[0].Url;
window.location.href = fileUrl;
} else {
alert('Não foi possível obter o link para o PDF.');
}
},
error: function() {
alert('Erro ao processar o pedido.');
},
complete: function() {
botaoPDF.html('<i class=\"fa fa-file-pdf-o\" aria-hidden=\"true\"></i> Download ');
spinner.hide();
botaoPDF.prop('disabled', false);
}
});
});
});
");
}
function converter_para_pdf() {
$urlPagina = $_POST['urlPagina'];
$API = 'xxxxxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://v2.convertapi.com/convert/web/to/pdf?Secret='.$API);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'Url' => $urlPagina,
'FileName' => 'ordem-de-servico',
'StoreFile' => 'true',
'HideElements' => 'button#converter-pdf',
'Scale' => '180',
'PageSize' => 'a4'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultado = curl_exec($ch);
curl_close($ch);
echo $resultado;
wp_die();
}
add_action('wp_ajax_converter_para_pdf', 'converter_para_pdf');
add_action('wp_ajax_nopriv_converter_para_pdf', 'converter_para_pdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment