S
S
sdarovich2020-06-01 11:57:20
JavaScript
sdarovich, 2020-06-01 11:57:20

How to download a file with saving styles to the content of the block?

I'm trying to save part of the page to a file, but when saving, all styles are lost.
The styles themselves are loaded from a third-party file, but I also tried to place them in the downloaded fragment through the tag style- the effect is the same.

The only option in which everything is displayed correctly in a Word file is to write all styles inline for each tag, but this is not an option at all.

I used the following code to save:

$(document).on('click','.load',function(e){
    var sourceHTML = `
    <html xmlns:o="urn:schemas-microsoft-com:office:office
        xmlns:w="urn:schemas-microsoft-com:office:word"
        xmlns="http://www.w3.org/TR/REC-html40">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Export</title>
    </head>
    <body>`+
        $('#added').html()+`
    </body>
    </html>`;
    var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
    $('<a>').attr({ 
        'href': source, 
        'download': 'filename.doc', 
        'id':'loader' }).prependTo('body');    
    $('#loader')[0].click();
    $('#loader')[0].remove();
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question