• 利用LibreOffice与ImageMagick将网页分享至微信


    现在越来越多的内容分享都是在微信上进行了。然而,若想将电脑浏览器中看到的感兴趣的网页分享至微信,则只能以纯文本的方式粘贴超级链接,而不能直接拷贝图文混排的HTML。因此,我想到不妨借助LibreOffice Writer和ImageMagick将网页转换成单张图片来分享,就如同微博上的长文章。具体做法如下。

    首先,将网页内容拷至LibreOffice Writer中保存。建议选用A6纸张大小,16号字体。

    然后,将其ODT文件导出为PDF格式。

    接下来,使用下面的脚本程序(pdf_to_single_img.sh)实现从PDF至单张图片的转换。在该脚本中,主要用到了ImageMagick的convert命令:首先,将PDF文档转化成多个图片文件;然后,使用-append选项,实现多个图片的纵向合并。需注意的是,图片文件名应按照version number排序(由sort -V来实现),才可以保证正确的页面顺序。

    #!/bin/bash
    
    script_name="pdf_to_single_img.sh"
    script_usage=$(cat <<EOF
    pdf_to_single_img.sh [OPTIONS] pdf_file
    EOF
    )
    script_function=$(cat <<EOF
    Convert a multipage PDF file into a single JPEG image for sharing.
    EOF
    )
    script_doc=$(cat <<EOF
    Script documentation.
    -d     Specify input image density.
    -q     Specify input image quality (0~100).
    -h     Display this help.
    EOF
    )
    script_examples=$(cat <<EOF
    pdf_to_single_img.sh -d 200 -q 100 input.pdf
    EOF
    )
    state_prefix="==="
    warning_prefix="***"
    error_prefix="!!!"
    
    function display_help() {
        if [ -n "$script_usage" ]; then
        echo -e "Usage: $script_usage"
        fi
        
        if [ -n "$script_function" ]; then
        echo -e "$script_function"
        fi
        
        if [ -n "$script_doc" ] ; then
        echo -e "
    $script_doc"
        fi
        
        if [ -n "$script_examples" ]; then
        echo -e "
    Examples"
        echo -e "$script_examples"
        fi
    }
    
    # Default input image density and quality
    img_density=150
    img_quality=80
    
    # Process command options
    while getopts ":hd:q:" opt; do
        case $opt in
        d  )  img_density=$OPTARG ;;
        q  )  img_quality=$OPTARG ;;
        h  )  display_help
            exit 0 ;;
        ? )  display_help
            exit 1 ;;
        esac
    done
    shift $(($OPTIND - 1))
    
    # Start execute the command
    output_img="${1%pdf}jpg"
    convert -density $img_density -quality $img_quality "$1" "$output_img"
    img_file_list=`find ./ -maxdepth 1 -name "${1%.pdf}*.jpg" | sort -V`
    convert $img_file_list -append "$output_img"
    echo "$img_file_list" | xargs rm

    最终生成的图片如下所示。

  • 相关阅读:
    java 的三种代理模式 (二)——子函数切面
    王者荣耀为什么不使用微服务架构,服务的极简主义,为什么交易网关使用redis做持久
    tcp_syncookies 半连接
    tcp_tw_recycle tcp_tw_reuse与timewait【yetdone】
    动态代理,没有被代理对象
    一次jstack解决update停顿
    动态代理反向
    注解的继承
    51单片机状态机键盘检测
    28335scififo中断接收与发送
  • 原文地址:https://www.cnblogs.com/quantumman/p/4753632.html
Copyright © 2020-2023  润新知